mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 20:23:55 +00:00
Adding CLI11_PARSE macro
This commit is contained in:
parent
aae40fbf0e
commit
0908251c76
@ -1,6 +1,7 @@
|
|||||||
## Version 1.2 (in progress)
|
## 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)
|
* 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)
|
* `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)
|
* Required positionals now take priority over subcommands [#23](https://github.com/CLIUtils/CLI11/issues/23)
|
||||||
|
@ -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 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:
|
The supported values are:
|
||||||
|
@ -13,11 +13,7 @@ int main(int argc, char **argv) {
|
|||||||
double value; // = 3.14;
|
double value; // = 3.14;
|
||||||
app.add_option("-d,--double", value, "Some Value");
|
app.add_option("-d,--double", value, "Some Value");
|
||||||
|
|
||||||
try {
|
CLI11_PARSE(app, argc, argv);
|
||||||
app.parse(argc, argv);
|
|
||||||
} catch(const CLI::Error &e) {
|
|
||||||
return app.exit(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "Working on file: " << file << ", direct count: " << app.count("--file")
|
std::cout << "Working on file: " << file << ", direct count: " << app.count("--file")
|
||||||
<< ", opt count: " << opt->count() << std::endl;
|
<< ", opt count: " << opt->count() << std::endl;
|
||||||
|
@ -23,14 +23,16 @@
|
|||||||
#include "CLI/StringTools.hpp"
|
#include "CLI/StringTools.hpp"
|
||||||
#include "CLI/TypeTools.hpp"
|
#include "CLI/TypeTools.hpp"
|
||||||
|
|
||||||
#define CLI11_PARSE(app,argc,argv) \
|
namespace CLI {
|
||||||
|
|
||||||
|
#ifndef CLI11_PARSE
|
||||||
|
#define CLI11_PARSE(app, argc, argv) \
|
||||||
try { \
|
try { \
|
||||||
(app).parse((argc),(argv)); \
|
(app).parse((argc), (argv)); \
|
||||||
} catch(const CLI::ParseError &e) { \
|
} catch(const CLI::ParseError &e) { \
|
||||||
return (app).exit(e); \
|
return (app).exit(e); \
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
namespace CLI {
|
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
enum class Classifer { NONE, POSITIONAL_MARK, SHORT, LONG, SUBCOMMAND };
|
enum class Classifer { NONE, POSITIONAL_MARK, SHORT, LONG, SUBCOMMAND };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user