From b0f67a06de3446aa97a4943ad0ad6086460b2b61 Mon Sep 17 00:00:00 2001 From: Tomas Barton Date: Thu, 13 Feb 2020 22:02:27 +0100 Subject: [PATCH] Code highlighting, add more examples (#220) --- README.md | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6ec80ac..f157052 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,26 @@ Additionally, anything after `--` will be parsed as a positional argument. ## Basics - #include +```cpp +#include +``` -Create a cxxopts::Options instance. +Create a `cxxopts::Options` instance. - cxxopts::Options options("MyProgram", "One line description of MyProgram"); +```cpp +cxxopts::Options options("MyProgram", "One line description of MyProgram"); +``` Then use `add_options`. - options.add_options() - ("d,debug", "Enable debugging") - ("f,file", "File name", cxxopts::value()) - ; +```cpp +options.add_options() + ("d,debug", "Enable debugging") // a bool parameter + ("i,integer", "Int param", cxxopts::value()) + ("f,file", "File name", cxxopts::value()) + ("v,verbose", "Verbose output", cxxopts::value()->default_value("false")) + ; +``` Options are declared with a long and an optional short option. A description must be provided. The third argument is the value, if omitted it is boolean. @@ -44,12 +52,16 @@ Any type can be given as long as it can be parsed, with operator>>. To parse the command line do: - auto result = options.parse(argc, argv); +```cpp +auto result = options.parse(argc, argv); +``` To retrieve an option use `result.count("option")` to get the number of times it appeared, and - result["opt"].as() +```cpp +result["opt"].as() +``` to get its value. If "opt" doesn't exist, or isn't of the right type, then an exception will be thrown. @@ -80,7 +92,9 @@ vector to the `help` function. Positional arguments can be optionally parsed into one or more options. To set up positional arguments, call - options.parse_positional({"first", "second", "last"}) +```cpp +options.parse_positional({"first", "second", "last"}) +``` where "last" should be the name of an option with a container type, and the others should have a single value. @@ -92,12 +106,16 @@ An option can be declared with a default or an implicit value, or both. A default value is the value that an option takes when it is not specified on the command line. The following specifies a default value for an option: - cxxopts::value()->default_value("value") +```cpp +cxxopts::value()->default_value("value") +``` An implicit value is the value that an option takes when it is given on the command line without an argument. The following specifies an implicit value: - cxxopts::value()->implicit_value("implicit") +```cpp +cxxopts::value()->implicit_value("implicit") +``` If an option had both, then not specifying it would give the value `"value"`, writing it on the command line as `--option` would give the value `"implicit"`, @@ -134,6 +152,39 @@ The string after the program name on the first line of the help can be completely replaced by calling `options.custom_help`. Note that you might also want to override the positional help by calling `options.positional_help`. + +## Example + +Putting all together: +```cpp +int main(int argc, char** argv) +{ + cxxopts::Options options("test", "A brief description"); + + options.add_options() + ("b,bar", "Param bar", cxxopts::value()) + ("d,debug", "Enable debugging", cxxopts::value()->default_value("false")) + ("f,foo", "Param foo", cxxopts::value()->default_value("10")) + ("h,help", "Print usage") + ; + + auto result = options.parse(argc, argv); + + if (result.count("help")) + { + std::cout << options.help() << std::endl; + exit(0); + } + bool debug = result["debug"].as(); + std::string bar; + if (result.count("bar")) + bar = result["bar"].as(); + int foo = result["foo"].as(); + + return 0; +} +``` + # Linking This is a header only library. -- libgit2 0.21.4