Commit 0908251c76c7024ec7009705ab0004516c04d9af

Authored by Henry Fredrick Schreiner
Committed by Henry Schreiner
1 parent aae40fbf

Adding CLI11_PARSE macro

CHANGELOG.md
1 1 ## Version 1.2 (in progress)
2 2  
3 3  
  4 +* Added `CLI11_PARSE(app, argc, argv)` macro for simple parse commands (does not support returning arg)
4 5 * The name string can now contain spaces around commas [#29](https://github.com/CLIUtils/CLI11/pull/29)
5 6 * `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)
6 7 * Required positionals now take priority over subcommands [#23](https://github.com/CLIUtils/CLI11/issues/23)
... ...
README.md
... ... @@ -101,6 +101,13 @@ try {
101 101 }
102 102 ```
103 103  
  104 +> Note: The final five lines are so common, they have a dedicated macro:
  105 +>
  106 +> ```cpp
  107 +CLI11_PARSE(app, argc, argv)
  108 +```
  109 +
  110 +
104 111 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")`.
105 112  
106 113 The supported values are:
... ...
examples/simple.cpp
... ... @@ -13,11 +13,7 @@ int main(int argc, char **argv) {
13 13 double value; // = 3.14;
14 14 app.add_option("-d,--double", value, "Some Value");
15 15  
16   - try {
17   - app.parse(argc, argv);
18   - } catch(const CLI::Error &e) {
19   - return app.exit(e);
20   - }
  16 + CLI11_PARSE(app, argc, argv);
21 17  
22 18 std::cout << "Working on file: " << file << ", direct count: " << app.count("--file")
23 19 << ", opt count: " << opt->count() << std::endl;
... ...
include/CLI/App.hpp
... ... @@ -23,15 +23,17 @@
23 23 #include "CLI/StringTools.hpp"
24 24 #include "CLI/TypeTools.hpp"
25 25  
26   -#define CLI11_PARSE(app,argc,argv) \
27   - try { \
28   - (app).parse((argc),(argv)); \
29   - } catch(const CLI::ParseError &e) { \
30   - return (app).exit(e); \
31   - }
32   -
33 26 namespace CLI {
34 27  
  28 +#ifndef CLI11_PARSE
  29 +#define CLI11_PARSE(app, argc, argv) \
  30 + try { \
  31 + (app).parse((argc), (argv)); \
  32 + } catch(const CLI::ParseError &e) { \
  33 + return (app).exit(e); \
  34 + }
  35 +#endif
  36 +
35 37 namespace detail {
36 38 enum class Classifer { NONE, POSITIONAL_MARK, SHORT, LONG, SUBCOMMAND };
37 39 struct AppFriend;
... ...