1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-01-15 06:38:02 +00:00

Compare commits

...

2 Commits

5 changed files with 80 additions and 7 deletions

View File

@ -787,8 +787,8 @@ The App class was designed allow toolkits to subclass it, to provide preset defa
but before run behavior, while
still giving the user freedom to `callback` on the main app.
The most important parse function is `parse(std::vector<std::string>)`, which takes a reversed list of arguments (so that `pop_back` processes the args in the correct order). `get_help_ptr` and `get_config_ptr` give you access to the help/config option pointers. The standard `parse` manually sets the name from the first argument, so it should not be in this vector. You can also use `parse(string, bool)` to split up and parse a string; the optional boolean should be set to true if you are
including the program name in the string, and false otherwise.
The most important parse function is `parse(std::vector<std::string>)`, which takes a reversed list of arguments (so that `pop_back` processes the args in the correct order). `get_help_ptr` and `get_config_ptr` give you access to the help/config option pointers. The standard `parse` manually sets the name from the first argument, so it should not be in this vector. You can also use `parse(string, bool)` to split up and parse a single string; the optional boolean should be set to true if you are
including the program name in the string, and false otherwise. The program name can contain spaces if it is an existing file, otherwise can be enclosed in quotes(single quote, double quote or backtick). Embedded quote characters can be escaped with `\`.
Also, in a related note, the `App` you get a pointer to is stored in the parent `App` in a `shared_ptr`s (similar to `Option`s) and are deleted when the main `App` goes out of scope unless the object has another owner.

View File

@ -2248,10 +2248,27 @@ class App {
/// Process callbacks and such.
void _process() {
_process_config_file();
_process_env();
CLI::FileError fe("ne");
bool caught_error{false};
try {
// the config file might generate a FileError but that should not be processed until later in the process
// to allow for help, version and other errors to generate first.
_process_config_file();
// process env shouldn't throw but no reason to process it if config generated an error
_process_env();
} catch(const CLI::FileError &fe2) {
fe = fe2;
caught_error = true;
}
// callbacks and help_flags can generate exceptions which should take priority over the config file error if one
// exists
_process_callbacks();
_process_help_flags();
if(caught_error) {
throw CLI::FileError(std::move(fe));
}
_process_requirements();
}

View File

@ -1100,12 +1100,36 @@ inline std::pair<std::string, std::string> split_program_name(std::string comman
if(esp == std::string::npos) {
// if we have reached the end and haven't found a valid file just assume the first argument is the
// program name
esp = commandline.find_first_of(' ', 1);
if(commandline[0] == '"' || commandline[0] == '\'' || commandline[0] == '`') {
bool embeddedQuote = false;
auto keyChar = commandline[0];
auto end = commandline.find_first_of(keyChar, 1);
while((end != std::string::npos) && (commandline[end - 1] == '\\')) { // deal with escaped quotes
end = commandline.find_first_of(keyChar, end + 1);
embeddedQuote = true;
}
if(end != std::string::npos) {
vals.first = commandline.substr(1, end - 1);
esp = end + 1;
if(embeddedQuote) {
vals.first = find_and_replace(vals.first, std::string("\\") + keyChar, std::string(1, keyChar));
embeddedQuote = false;
}
} else {
esp = commandline.find_first_of(' ', 1);
}
} else {
esp = commandline.find_first_of(' ', 1);
}
break;
}
}
vals.first = commandline.substr(0, esp);
rtrim(vals.first);
if(vals.first.empty()) {
vals.first = commandline.substr(0, esp);
rtrim(vals.first);
}
// strip the program name
vals.second = (esp != std::string::npos) ? commandline.substr(esp + 1) : std::string{};
ltrim(vals.second);

View File

@ -535,6 +535,10 @@ TEST_CASE_METHOD(TApp, "IniRequiredNoDefault", "[config]") {
int two{0};
app.add_option("--two", two);
REQUIRE_THROWS_AS(run(), CLI::FileError);
// test to make sure help still gets called correctly
// Github issue #533 https://github.com/CLIUtils/CLI11/issues/553
args = {"--help"};
REQUIRE_THROWS_AS(run(), CLI::CallForHelp);
}
TEST_CASE_METHOD(TApp, "IniNotRequiredNoDefault", "[config]") {

View File

@ -78,3 +78,31 @@ TEST_CASE_METHOD(TApp, "ExistingExeCheckWithLotsOfSpace", "[stringparse]") {
CHECK(std::string("./") + std::string(tmpexe) == app.get_name());
}
// From Github issue #591 https://github.com/CLIUtils/CLI11/issues/591
TEST_CASE_METHOD(TApp, "ProgNameWithSpace", "[stringparse]") {
app.add_flag("--foo");
CHECK_NOTHROW(app.parse("\"Foo Bar\" --foo", true));
CHECK(app["--foo"]->as<bool>());
CHECK(app.get_name() == "Foo Bar");
}
TEST_CASE_METHOD(TApp, "ProgNameWithSpaceEmbeddedQuote", "[stringparse]") {
app.add_flag("--foo");
CHECK_NOTHROW(app.parse("\"Foo\\\" Bar\" --foo", true));
CHECK(app["--foo"]->as<bool>());
CHECK(app.get_name() == "Foo\" Bar");
}
TEST_CASE_METHOD(TApp, "ProgNameWithSpaceSingleQuote", "[stringparse]") {
app.add_flag("--foo");
CHECK_NOTHROW(app.parse(R"('Foo\' Bar' --foo)", true));
CHECK(app["--foo"]->as<bool>());
CHECK(app.get_name() == "Foo' Bar");
}