diff --git a/README.md b/README.md index ef17b49..ae0378b 100644 --- a/README.md +++ b/README.md @@ -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)`, 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)`, 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. diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index 90b590f..474350f 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -1100,12 +1100,36 @@ inline std::pair 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); diff --git a/tests/StringParseTest.cpp b/tests/StringParseTest.cpp index 3a9f737..08f3a8e 100644 --- a/tests/StringParseTest.cpp +++ b/tests/StringParseTest.cpp @@ -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()); + 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()); + 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()); + CHECK(app.get_name() == "Foo' Bar"); +}