Skip to content

Commit b8edd50

Browse files
authored
fix: drop one arg parse shortcut (#987)
1 parent 1e4f7f9 commit b8edd50

File tree

8 files changed

+1
-156
lines changed

8 files changed

+1
-156
lines changed

include/CLI/App.hpp

-4
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,6 @@ class App {
848848
/// Reset the parsed data
849849
void clear();
850850

851-
/// Parse the command-line arguments passed to the main function of the executable.
852-
/// This overload will correctly parse unicode arguments on Windows.
853-
void parse();
854-
855851
/// Parses the command line - throws errors.
856852
/// This must be called after the options are in but before the rest of the program.
857853
void parse(int argc, const char *const *argv);

include/CLI/Argv.hpp

-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ namespace detail {
2121
CLI11_INLINE std::vector<std::string> compute_win32_argv();
2222
#endif
2323
} // namespace detail
24-
25-
/// argc as passed in to this executable.
26-
CLI11_INLINE int argc();
27-
28-
/// argv as passed in to this executable, converted to utf-8 on Windows.
29-
CLI11_INLINE const char *const *argv();
30-
3124
// [CLI11:argv_hpp:end]
3225
} // namespace CLI
3326

include/CLI/impl/App_inl.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,6 @@ CLI11_INLINE void App::clear() {
529529
}
530530
}
531531

532-
CLI11_INLINE void App::parse() { parse(argc(), argv()); } // LCOV_EXCL_LINE
533-
534532
CLI11_INLINE void App::parse(int argc, const char *const *argv) { parse_char_t(argc, argv); }
535533
CLI11_INLINE void App::parse(int argc, const wchar_t *const *argv) { parse_char_t(argc, argv); }
536534

include/CLI/impl/Argv_inl.hpp

-101
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@
5151
// third
5252
#include <processthreadsapi.h>
5353
#include <shellapi.h>
54-
55-
#elif defined(__APPLE__)
56-
#include <crt_externs.h>
5754
#endif
5855
// [CLI11:argv_inl_includes:end]
5956

@@ -62,33 +59,6 @@ namespace CLI {
6259

6360
namespace detail {
6461

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-
9262
#ifdef _WIN32
9363
CLI11_INLINE std::vector<std::string> compute_win32_argv() {
9464
std::vector<std::string> result;
@@ -112,78 +82,7 @@ CLI11_INLINE std::vector<std::string> compute_win32_argv() {
11282
}
11383
#endif
11484

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-
18385
} // namespace detail
18486

185-
CLI11_INLINE const char *const *argv() { return detail::args().data(); }
186-
CLI11_INLINE int argc() { return static_cast<int>(detail::args().size()); }
187-
18887
// [CLI11:argv_inl_hpp:end]
18988
} // namespace CLI

tests/AppTest.cpp

-18
Original file line numberDiff line numberDiff line change
@@ -2628,24 +2628,6 @@ TEST_CASE("C20_compile", "simple") {
26282628
CHECK_FALSE(flag->empty());
26292629
}
26302630

2631-
// #14
2632-
TEST_CASE("System Args", "[app]") {
2633-
const char *commandline = CLI11_SYSTEM_ARGS_EXE " 1234 false \"hello world\"";
2634-
int retval = std::system(commandline);
2635-
2636-
if(retval == -1) {
2637-
FAIL("Executable '" << commandline << "' reported different argc count");
2638-
}
2639-
2640-
if(retval > 0) {
2641-
FAIL("Executable '" << commandline << "' reported different argv at index " << (retval - 1));
2642-
}
2643-
2644-
if(retval != 0) {
2645-
FAIL("Executable '" << commandline << "' failed with an unknown return code");
2646-
}
2647-
}
2648-
26492631
// #845
26502632
TEST_CASE("Ensure UTF-8", "[app]") {
26512633
const char *commandline = CLI11_ENSURE_UTF8_EXE " 1234 false \"hello world\"";

tests/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ endforeach()
119119
add_custom_target(cli11_test_data DEPENDS ${DATA_FILES})
120120

121121
# Build dependent applications which are launched from test code
122-
set(CLI11_DEPENDENT_APPLICATIONS system_args ensure_utf8 ensure_utf8_twice)
122+
set(CLI11_DEPENDENT_APPLICATIONS ensure_utf8 ensure_utf8_twice)
123123

124124
foreach(APP IN LISTS CLI11_DEPENDENT_APPLICATIONS)
125125
add_executable(${APP} applications/${APP}.cpp)

tests/applications/system_args.cpp

-22
This file was deleted.

tests/meson.build

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ testnames = [
6565
]
6666

6767
dependent_applications = [
68-
'system_args',
6968
'ensure_utf8',
7069
'ensure_utf8_twice',
7170
]

0 commit comments

Comments
 (0)