Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for spaces in names issue #29

Merged
merged 3 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 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)
* Extra requirements enforced by Travis
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions examples/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@

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;
Expand Down
4 changes: 2 additions & 2 deletions include/CLI/Split.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ inline std::vector<std::string> split_names(std::string current) {
std::vector<std::string> 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;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/CreationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,21 @@ 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"));
}
3 changes: 3 additions & 0 deletions tests/HelpersTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ TEST(Split, StringList) {

std::vector<std::string> 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<std::string>({"one"}), CLI::detail::split_names("one"));
}
Expand Down