Skip to content

Commit

Permalink
fix: drop one arg parse shortcut (#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii authored Jan 31, 2024
1 parent 1e4f7f9 commit b8edd50
Show file tree
Hide file tree
Showing 8 changed files with 1 addition and 156 deletions.
4 changes: 0 additions & 4 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,10 +848,6 @@ class App {
/// Reset the parsed data
void clear();

/// Parse the command-line arguments passed to the main function of the executable.
/// This overload will correctly parse unicode arguments on Windows.
void parse();

/// Parses the command line - throws errors.
/// This must be called after the options are in but before the rest of the program.
void parse(int argc, const char *const *argv);
Expand Down
7 changes: 0 additions & 7 deletions include/CLI/Argv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ namespace detail {
CLI11_INLINE std::vector<std::string> compute_win32_argv();
#endif
} // namespace detail

/// argc as passed in to this executable.
CLI11_INLINE int argc();

/// argv as passed in to this executable, converted to utf-8 on Windows.
CLI11_INLINE const char *const *argv();

// [CLI11:argv_hpp:end]
} // namespace CLI

Expand Down
2 changes: 0 additions & 2 deletions include/CLI/impl/App_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,6 @@ CLI11_INLINE void App::clear() {
}
}

CLI11_INLINE void App::parse() { parse(argc(), argv()); } // LCOV_EXCL_LINE

CLI11_INLINE void App::parse(int argc, const char *const *argv) { parse_char_t(argc, argv); }
CLI11_INLINE void App::parse(int argc, const wchar_t *const *argv) { parse_char_t(argc, argv); }

Expand Down
101 changes: 0 additions & 101 deletions include/CLI/impl/Argv_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
// third
#include <processthreadsapi.h>
#include <shellapi.h>

#elif defined(__APPLE__)
#include <crt_externs.h>
#endif
// [CLI11:argv_inl_includes:end]

Expand All @@ -62,33 +59,6 @@ namespace CLI {

namespace detail {

#ifdef __APPLE__
// Copy argc and argv as early as possible to avoid modification
static const std::vector<const char *> static_args = [] {
static const std::vector<std::string> static_args_as_strings = [] {
std::vector<std::string> args_as_strings;
int argc = *_NSGetArgc();
char **argv = *_NSGetArgv();

args_as_strings.reserve(static_cast<size_t>(argc));
for(size_t i = 0; i < static_cast<size_t>(argc); i++) {
args_as_strings.push_back(argv[i]);
}

return args_as_strings;
}();

std::vector<const char *> static_args_result;
static_args_result.reserve(static_args_as_strings.size());

for(const auto &arg : static_args_as_strings) {
static_args_result.push_back(arg.data());
}

return static_args_result;
}();
#endif

#ifdef _WIN32
CLI11_INLINE std::vector<std::string> compute_win32_argv() {
std::vector<std::string> result;
Expand All @@ -112,78 +82,7 @@ CLI11_INLINE std::vector<std::string> compute_win32_argv() {
}
#endif

/// Command-line arguments, as passed in to this executable, converted to utf-8 on Windows.
CLI11_INLINE const std::vector<const char *> &args() {
// This function uses initialization via lambdas extensively to take advantage of the thread safety of static
// variable initialization [stmt.dcl.3]

#ifdef _WIN32
static const std::vector<const char *> static_args = [] {
static const std::vector<std::string> static_args_as_strings = compute_win32_argv();

std::vector<const char *> static_args_result;
static_args_result.reserve(static_args_as_strings.size());

for(const auto &arg : static_args_as_strings) {
static_args_result.push_back(arg.data());
}

return static_args_result;
}();

return static_args;

#elif defined(__APPLE__)

return static_args;

#else
static const std::vector<const char *> static_args = [] {
static const std::vector<char> static_cmdline = [] {
// On posix, retrieve arguments from /proc/self/cmdline, separated by null terminators.
std::vector<char> cmdline;

auto deleter = [](FILE *f) { std::fclose(f); };
std::unique_ptr<FILE, decltype(deleter)> fp_unique(std::fopen("/proc/self/cmdline", "r"), deleter);
FILE *fp = fp_unique.get();
if(!fp) {
throw std::runtime_error("could not open /proc/self/cmdline for reading"); // LCOV_EXCL_LINE
}

size_t size = 0;
while(std::feof(fp) == 0) {
cmdline.resize(size + 128);
size += std::fread(cmdline.data() + size, 1, 128, fp);

if(std::ferror(fp) != 0) {
throw std::runtime_error("error during reading /proc/self/cmdline"); // LCOV_EXCL_LINE
}
}
cmdline.resize(size);

return cmdline;
}();

std::size_t argc = static_cast<std::size_t>(std::count(static_cmdline.begin(), static_cmdline.end(), '\0'));
std::vector<const char *> static_args_result;
static_args_result.reserve(argc);

for(auto it = static_cmdline.begin(); it != static_cmdline.end();
it = std::find(it, static_cmdline.end(), '\0') + 1) {
static_args_result.push_back(static_cmdline.data() + (it - static_cmdline.begin()));
}

return static_args_result;
}();

return static_args;
#endif
}

} // namespace detail

CLI11_INLINE const char *const *argv() { return detail::args().data(); }
CLI11_INLINE int argc() { return static_cast<int>(detail::args().size()); }

// [CLI11:argv_inl_hpp:end]
} // namespace CLI
18 changes: 0 additions & 18 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2628,24 +2628,6 @@ TEST_CASE("C20_compile", "simple") {
CHECK_FALSE(flag->empty());
}

// #14
TEST_CASE("System Args", "[app]") {
const char *commandline = CLI11_SYSTEM_ARGS_EXE " 1234 false \"hello world\"";
int retval = std::system(commandline);

if(retval == -1) {
FAIL("Executable '" << commandline << "' reported different argc count");
}

if(retval > 0) {
FAIL("Executable '" << commandline << "' reported different argv at index " << (retval - 1));
}

if(retval != 0) {
FAIL("Executable '" << commandline << "' failed with an unknown return code");
}
}

// #845
TEST_CASE("Ensure UTF-8", "[app]") {
const char *commandline = CLI11_ENSURE_UTF8_EXE " 1234 false \"hello world\"";
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ endforeach()
add_custom_target(cli11_test_data DEPENDS ${DATA_FILES})

# Build dependent applications which are launched from test code
set(CLI11_DEPENDENT_APPLICATIONS system_args ensure_utf8 ensure_utf8_twice)
set(CLI11_DEPENDENT_APPLICATIONS ensure_utf8 ensure_utf8_twice)

foreach(APP IN LISTS CLI11_DEPENDENT_APPLICATIONS)
add_executable(${APP} applications/${APP}.cpp)
Expand Down
22 changes: 0 additions & 22 deletions tests/applications/system_args.cpp

This file was deleted.

1 change: 0 additions & 1 deletion tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ testnames = [
]

dependent_applications = [
'system_args',
'ensure_utf8',
'ensure_utf8_twice',
]
Expand Down

0 comments on commit b8edd50

Please sign in to comment.