1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Adding CLI11_PARSE macro

This commit is contained in:
Henry Fredrick Schreiner 2017-09-05 23:09:43 -04:00 committed by Henry Schreiner
parent aae40fbf0e
commit 0908251c76
4 changed files with 18 additions and 12 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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;

View File

@ -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;