From e02034eda5a14e6202e2561c5d43400cf5566db0 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 5 Sep 2017 17:38:02 -0400 Subject: [PATCH 1/3] Fix for spaces in names issue --- include/CLI/Split.hpp | 4 ++-- tests/AppTest.cpp | 1 + tests/CreationTest.cpp | 21 +++++++++++++++++++++ tests/HelpersTest.cpp | 3 +++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/CLI/Split.hpp b/include/CLI/Split.hpp index f7fb5abd2..f2f3ba105 100644 --- a/include/CLI/Split.hpp +++ b/include/CLI/Split.hpp @@ -44,10 +44,10 @@ inline std::vector split_names(std::string current) { std::vector output; size_t val; while((val = current.find(",")) != std::string::npos) { - output.push_back(current.substr(0, val)); + output.push_back(trim_copy(current.substr(0, val))); current = current.substr(val + 1); } - output.push_back(current); + output.push_back(trim_copy(current)); return output; } diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 2ddb4be7c..c535f9438 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -260,6 +260,7 @@ TEST_F(TApp, Positionals) { EXPECT_EQ("thing2", posit2); } + TEST_F(TApp, ForcedPositional) { std::vector posit; auto one = app.add_flag("--one"); diff --git a/tests/CreationTest.cpp b/tests/CreationTest.cpp index c7f391c6e..8f1a7337a 100644 --- a/tests/CreationTest.cpp +++ b/tests/CreationTest.cpp @@ -230,3 +230,24 @@ TEST_F(TApp, CheckNameNoCase) { EXPECT_TRUE(pos2->check_name("pOs2")); EXPECT_TRUE(pos2->check_name("pos2")); } + + +TEST_F(TApp, PreSpaces) { + int x; + auto myapp = app.add_option(" -a, --long, other", x); + + EXPECT_TRUE(myapp->check_lname("long")); + EXPECT_TRUE(myapp->check_sname("a")); + EXPECT_TRUE(myapp->check_name("other")); + +} + +TEST_F(TApp, AllSpaces) { + int x; + auto myapp = app.add_option(" -a , --long , other ", x); + + EXPECT_TRUE(myapp->check_lname("long")); + EXPECT_TRUE(myapp->check_sname("a")); + EXPECT_TRUE(myapp->check_name("other")); + +} diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index 59175f5d8..a94583acc 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -179,6 +179,9 @@ TEST(Split, StringList) { std::vector results{"a", "long", "--lone", "-q"}; EXPECT_EQ(results, CLI::detail::split_names("a,long,--lone,-q")); + EXPECT_EQ(results, CLI::detail::split_names(" a, long, --lone, -q")); + EXPECT_EQ(results, CLI::detail::split_names(" a , long , --lone , -q ")); + EXPECT_EQ(results, CLI::detail::split_names(" a , long , --lone , -q ")); EXPECT_EQ(std::vector({"one"}), CLI::detail::split_names("one")); } From 8cf27ac08b22585840fe2c2988ee218094a20ecc Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 5 Sep 2017 23:06:10 -0400 Subject: [PATCH 2/3] Whitespace fixes --- CHANGELOG.md | 1 + include/CLI/App.hpp | 7 +++++++ tests/AppTest.cpp | 1 - tests/CreationTest.cpp | 7 ++----- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf1fb2517..a9f92e638 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Version 1.2 (in progress) +* The name string can now contain spaces around commas [#29](https://github.com/CLIUtils/CLI11/pull/29) * `set_default_str` now only sets string, and `set_default_val` will evaluate the default string given [#26](https://github.com/CLIUtils/CLI11/issues/26) * Required positionals now take priority over subcommands [#23](https://github.com/CLIUtils/CLI11/issues/23) * Extra requirements enforced by Travis diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index cdb91abc0..dfec4a4a9 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -23,6 +23,13 @@ #include "CLI/StringTools.hpp" #include "CLI/TypeTools.hpp" +#define CLI11_PARSE(app,argc,argv) \ + try { \ + (app).parse((argc),(argv)); \ + } catch(const CLI::ParseError &e) { \ + return (app).exit(e); \ + } + namespace CLI { namespace detail { diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index c535f9438..2ddb4be7c 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -260,7 +260,6 @@ TEST_F(TApp, Positionals) { EXPECT_EQ("thing2", posit2); } - TEST_F(TApp, ForcedPositional) { std::vector posit; auto one = app.add_flag("--one"); diff --git a/tests/CreationTest.cpp b/tests/CreationTest.cpp index 8f1a7337a..a1c7b9c7e 100644 --- a/tests/CreationTest.cpp +++ b/tests/CreationTest.cpp @@ -231,23 +231,20 @@ TEST_F(TApp, CheckNameNoCase) { EXPECT_TRUE(pos2->check_name("pos2")); } - TEST_F(TApp, PreSpaces) { int x; auto myapp = app.add_option(" -a, --long, other", x); - + EXPECT_TRUE(myapp->check_lname("long")); EXPECT_TRUE(myapp->check_sname("a")); EXPECT_TRUE(myapp->check_name("other")); - } TEST_F(TApp, AllSpaces) { int x; auto myapp = app.add_option(" -a , --long , other ", x); - + EXPECT_TRUE(myapp->check_lname("long")); EXPECT_TRUE(myapp->check_sname("a")); EXPECT_TRUE(myapp->check_name("other")); - } From 1dc7a188c833853219f8b8d1e49cc1026668e929 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 5 Sep 2017 23:09:43 -0400 Subject: [PATCH 3/3] Adding CLI11_PARSE macro --- CHANGELOG.md | 1 + README.md | 7 +++++++ examples/simple.cpp | 6 +----- include/CLI/App.hpp | 16 +++++++++------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9f92e638..7be7f31fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Version 1.2 (in progress) +* Added `CLI11_PARSE(app, argc, argv)` macro for simple parse commands (does not support returning arg) * The name string can now contain spaces around commas [#29](https://github.com/CLIUtils/CLI11/pull/29) * `set_default_str` now only sets string, and `set_default_val` will evaluate the default string given [#26](https://github.com/CLIUtils/CLI11/issues/26) * Required positionals now take priority over subcommands [#23](https://github.com/CLIUtils/CLI11/issues/23) diff --git a/README.md b/README.md index df4c06513..ef841bad7 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,13 @@ try { } ``` +> Note: The final five lines are so common, they have a dedicated macro: +> +> ```cpp +CLI11_PARSE(app, argc, argv) +``` + + The initialization is just one line, adding options is just two each. The try/catch block ensures that `-h,--help` or a parse error will exit with the correct return code (selected from `CLI::ExitCodes`). (The return here should be inside `main`). After the app runs, the filename will be set to the correct value if it was passed, otherwise it will be set to the default. You can check to see if this was passed on the command line with `app.count("--file")`. The supported values are: diff --git a/examples/simple.cpp b/examples/simple.cpp index 505af06f3..70bd87c4c 100644 --- a/examples/simple.cpp +++ b/examples/simple.cpp @@ -13,11 +13,7 @@ int main(int argc, char **argv) { double value; // = 3.14; app.add_option("-d,--double", value, "Some Value"); - try { - app.parse(argc, argv); - } catch(const CLI::Error &e) { - return app.exit(e); - } + CLI11_PARSE(app, argc, argv); std::cout << "Working on file: " << file << ", direct count: " << app.count("--file") << ", opt count: " << opt->count() << std::endl; diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index dfec4a4a9..ba2adc730 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -23,15 +23,17 @@ #include "CLI/StringTools.hpp" #include "CLI/TypeTools.hpp" -#define CLI11_PARSE(app,argc,argv) \ - try { \ - (app).parse((argc),(argv)); \ - } catch(const CLI::ParseError &e) { \ - return (app).exit(e); \ - } - namespace CLI { +#ifndef CLI11_PARSE +#define CLI11_PARSE(app, argc, argv) \ + try { \ + (app).parse((argc), (argv)); \ + } catch(const CLI::ParseError &e) { \ + return (app).exit(e); \ + } +#endif + namespace detail { enum class Classifer { NONE, POSITIONAL_MARK, SHORT, LONG, SUBCOMMAND }; struct AppFriend;