mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-30 20:53:52 +00:00
fix: drop one arg parse shortcut (#987)
This commit is contained in:
parent
1e4f7f9890
commit
b8edd50acf
@ -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);
|
||||
|
@ -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
|
||||
|
||||
|
@ -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); }
|
||||
|
||||
|
@ -51,9 +51,6 @@
|
||||
// third
|
||||
#include <processthreadsapi.h>
|
||||
#include <shellapi.h>
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
#include <crt_externs.h>
|
||||
#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<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;
|
||||
@ -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
|
||||
|
@ -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\"";
|
||||
|
@ -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)
|
||||
|
@ -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 <CLI/CLI.hpp>
|
||||
#include <cstring>
|
||||
|
||||
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;
|
||||
}
|
@ -65,7 +65,6 @@ testnames = [
|
||||
]
|
||||
|
||||
dependent_applications = [
|
||||
'system_args',
|
||||
'ensure_utf8',
|
||||
'ensure_utf8_twice',
|
||||
]
|
||||
|
Loading…
x
Reference in New Issue
Block a user