Skip to content

Commit

Permalink
Adding CLI11_PARSE macro
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Sep 6, 2017
1 parent aae40fb commit 0908251
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
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
16 changes: 9 additions & 7 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0908251

Please sign in to comment.