From b8edd50acfc627820e22cf001224c311cefd1b9c Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 30 Jan 2024 22:03:20 -0500 Subject: [PATCH] fix: drop one arg parse shortcut (#987) --- include/CLI/App.hpp | 4 -- include/CLI/Argv.hpp | 7 -- include/CLI/impl/App_inl.hpp | 2 - include/CLI/impl/Argv_inl.hpp | 101 ----------------------------- tests/AppTest.cpp | 18 ----- tests/CMakeLists.txt | 2 +- tests/applications/system_args.cpp | 22 ------- tests/meson.build | 1 - 8 files changed, 1 insertion(+), 156 deletions(-) delete mode 100644 tests/applications/system_args.cpp diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index b63be47e7..9ba9c22aa 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -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); diff --git a/include/CLI/Argv.hpp b/include/CLI/Argv.hpp index 545bd58d1..619a7abf9 100644 --- a/include/CLI/Argv.hpp +++ b/include/CLI/Argv.hpp @@ -21,13 +21,6 @@ namespace detail { CLI11_INLINE std::vector 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 diff --git a/include/CLI/impl/App_inl.hpp b/include/CLI/impl/App_inl.hpp index ae8b5f339..e33729e38 100644 --- a/include/CLI/impl/App_inl.hpp +++ b/include/CLI/impl/App_inl.hpp @@ -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); } diff --git a/include/CLI/impl/Argv_inl.hpp b/include/CLI/impl/Argv_inl.hpp index 620f1fb73..b3c328b1a 100644 --- a/include/CLI/impl/Argv_inl.hpp +++ b/include/CLI/impl/Argv_inl.hpp @@ -51,9 +51,6 @@ // third #include #include - -#elif defined(__APPLE__) -#include #endif // [CLI11:argv_inl_includes:end] @@ -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 static_args = [] { - static const std::vector static_args_as_strings = [] { - std::vector args_as_strings; - int argc = *_NSGetArgc(); - char **argv = *_NSGetArgv(); - - args_as_strings.reserve(static_cast(argc)); - for(size_t i = 0; i < static_cast(argc); i++) { - args_as_strings.push_back(argv[i]); - } - - return args_as_strings; - }(); - - std::vector 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 compute_win32_argv() { std::vector result; @@ -112,78 +82,7 @@ CLI11_INLINE std::vector compute_win32_argv() { } #endif -/// Command-line arguments, as passed in to this executable, converted to utf-8 on Windows. -CLI11_INLINE const std::vector &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 static_args = [] { - static const std::vector static_args_as_strings = compute_win32_argv(); - - std::vector 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 static_args = [] { - static const std::vector static_cmdline = [] { - // On posix, retrieve arguments from /proc/self/cmdline, separated by null terminators. - std::vector cmdline; - - auto deleter = [](FILE *f) { std::fclose(f); }; - std::unique_ptr 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::count(static_cmdline.begin(), static_cmdline.end(), '\0')); - std::vector 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(detail::args().size()); } - // [CLI11:argv_inl_hpp:end] } // namespace CLI diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 4e716a8e3..fd72ecd7b 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -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\""; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 621584807..7d0ced99f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/applications/system_args.cpp b/tests/applications/system_args.cpp deleted file mode 100644 index 2cad18b19..000000000 --- a/tests/applications/system_args.cpp +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner -// under NSF AWARD 1414736 and by the respective contributors. -// All rights reserved. -// -// SPDX-License-Identifier: BSD-3-Clause - -#include -#include - -int main(int argc, char **argv) { - if(argc != CLI::argc()) { - return -1; - } - - for(int i = 0; i < argc; i++) { - if(std::strcmp(argv[i], CLI::argv()[i]) != 0) { - return i + 1; - } - } - - return 0; -} diff --git a/tests/meson.build b/tests/meson.build index 2f1c0ad27..383d03a44 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -65,7 +65,6 @@ testnames = [ ] dependent_applications = [ - 'system_args', 'ensure_utf8', 'ensure_utf8_twice', ]