From 1dc7a188c833853219f8b8d1e49cc1026668e929 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 5 Sep 2017 23:09:43 -0400 Subject: [PATCH] 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;