51
51
// third
52
52
#include < processthreadsapi.h>
53
53
#include < shellapi.h>
54
-
55
- #elif defined(__APPLE__)
56
- #include < crt_externs.h>
57
54
#endif
58
55
// [CLI11:argv_inl_includes:end]
59
56
@@ -62,33 +59,6 @@ namespace CLI {
62
59
63
60
namespace detail {
64
61
65
- #ifdef __APPLE__
66
- // Copy argc and argv as early as possible to avoid modification
67
- static const std::vector<const char *> static_args = [] {
68
- static const std::vector<std::string> static_args_as_strings = [] {
69
- std::vector<std::string> args_as_strings;
70
- int argc = *_NSGetArgc ();
71
- char **argv = *_NSGetArgv ();
72
-
73
- args_as_strings.reserve (static_cast <size_t >(argc));
74
- for (size_t i = 0 ; i < static_cast <size_t >(argc); i++) {
75
- args_as_strings.push_back (argv[i]);
76
- }
77
-
78
- return args_as_strings;
79
- }();
80
-
81
- std::vector<const char *> static_args_result;
82
- static_args_result.reserve (static_args_as_strings.size ());
83
-
84
- for (const auto &arg : static_args_as_strings) {
85
- static_args_result.push_back (arg.data ());
86
- }
87
-
88
- return static_args_result;
89
- }();
90
- #endif
91
-
92
62
#ifdef _WIN32
93
63
CLI11_INLINE std::vector<std::string> compute_win32_argv () {
94
64
std::vector<std::string> result;
@@ -112,78 +82,7 @@ CLI11_INLINE std::vector<std::string> compute_win32_argv() {
112
82
}
113
83
#endif
114
84
115
- // / Command-line arguments, as passed in to this executable, converted to utf-8 on Windows.
116
- CLI11_INLINE const std::vector<const char *> &args () {
117
- // This function uses initialization via lambdas extensively to take advantage of the thread safety of static
118
- // variable initialization [stmt.dcl.3]
119
-
120
- #ifdef _WIN32
121
- static const std::vector<const char *> static_args = [] {
122
- static const std::vector<std::string> static_args_as_strings = compute_win32_argv ();
123
-
124
- std::vector<const char *> static_args_result;
125
- static_args_result.reserve (static_args_as_strings.size ());
126
-
127
- for (const auto &arg : static_args_as_strings) {
128
- static_args_result.push_back (arg.data ());
129
- }
130
-
131
- return static_args_result;
132
- }();
133
-
134
- return static_args;
135
-
136
- #elif defined(__APPLE__)
137
-
138
- return static_args;
139
-
140
- #else
141
- static const std::vector<const char *> static_args = [] {
142
- static const std::vector<char > static_cmdline = [] {
143
- // On posix, retrieve arguments from /proc/self/cmdline, separated by null terminators.
144
- std::vector<char > cmdline;
145
-
146
- auto deleter = [](FILE *f) { std::fclose (f); };
147
- std::unique_ptr<FILE, decltype (deleter)> fp_unique (std::fopen (" /proc/self/cmdline" , " r" ), deleter);
148
- FILE *fp = fp_unique.get ();
149
- if (!fp) {
150
- throw std::runtime_error (" could not open /proc/self/cmdline for reading" ); // LCOV_EXCL_LINE
151
- }
152
-
153
- size_t size = 0 ;
154
- while (std::feof (fp) == 0 ) {
155
- cmdline.resize (size + 128 );
156
- size += std::fread (cmdline.data () + size, 1 , 128 , fp);
157
-
158
- if (std::ferror (fp) != 0 ) {
159
- throw std::runtime_error (" error during reading /proc/self/cmdline" ); // LCOV_EXCL_LINE
160
- }
161
- }
162
- cmdline.resize (size);
163
-
164
- return cmdline;
165
- }();
166
-
167
- std::size_t argc = static_cast <std::size_t >(std::count (static_cmdline.begin (), static_cmdline.end (), ' \0 ' ));
168
- std::vector<const char *> static_args_result;
169
- static_args_result.reserve (argc);
170
-
171
- for (auto it = static_cmdline.begin (); it != static_cmdline.end ();
172
- it = std::find (it, static_cmdline.end (), ' \0 ' ) + 1 ) {
173
- static_args_result.push_back (static_cmdline.data () + (it - static_cmdline.begin ()));
174
- }
175
-
176
- return static_args_result;
177
- }();
178
-
179
- return static_args;
180
- #endif
181
- }
182
-
183
85
} // namespace detail
184
86
185
- CLI11_INLINE const char *const *argv () { return detail::args ().data (); }
186
- CLI11_INLINE int argc () { return static_cast <int >(detail::args ().size ()); }
187
-
188
87
// [CLI11:argv_inl_hpp:end]
189
88
} // namespace CLI
0 commit comments