Commit b0f67a06de3446aa97a4943ad0ad6086460b2b61

Authored by Tomas Barton
Committed by GitHub
1 parent 6fa46a74

Code highlighting, add more examples (#220)

Showing 1 changed file with 63 additions and 12 deletions
README.md
... ... @@ -25,18 +25,26 @@ Additionally, anything after `--` will be parsed as a positional argument.
25 25  
26 26 ## Basics
27 27  
28   - #include <cxxopts.hpp>
  28 +```cpp
  29 +#include <cxxopts.hpp>
  30 +```
29 31  
30   -Create a cxxopts::Options instance.
  32 +Create a `cxxopts::Options` instance.
31 33  
32   - cxxopts::Options options("MyProgram", "One line description of MyProgram");
  34 +```cpp
  35 +cxxopts::Options options("MyProgram", "One line description of MyProgram");
  36 +```
33 37  
34 38 Then use `add_options`.
35 39  
36   - options.add_options()
37   - ("d,debug", "Enable debugging")
38   - ("f,file", "File name", cxxopts::value<std::string>())
39   - ;
  40 +```cpp
  41 +options.add_options()
  42 + ("d,debug", "Enable debugging") // a bool parameter
  43 + ("i,integer", "Int param", cxxopts::value<int>())
  44 + ("f,file", "File name", cxxopts::value<std::string>())
  45 + ("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false"))
  46 + ;
  47 +```
40 48  
41 49 Options are declared with a long and an optional short option. A description
42 50 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&gt;&gt;.
44 52  
45 53 To parse the command line do:
46 54  
47   - auto result = options.parse(argc, argv);
  55 +```cpp
  56 +auto result = options.parse(argc, argv);
  57 +```
48 58  
49 59 To retrieve an option use `result.count("option")` to get the number of times
50 60 it appeared, and
51 61  
52   - result["opt"].as<type>()
  62 +```cpp
  63 +result["opt"].as<type>()
  64 +```
53 65  
54 66 to get its value. If "opt" doesn't exist, or isn't of the right type, then an
55 67 exception will be thrown.
... ... @@ -80,7 +92,9 @@ vector to the `help` function.
80 92 Positional arguments can be optionally parsed into one or more options.
81 93 To set up positional arguments, call
82 94  
83   - options.parse_positional({"first", "second", "last"})
  95 +```cpp
  96 +options.parse_positional({"first", "second", "last"})
  97 +```
84 98  
85 99 where "last" should be the name of an option with a container type, and the
86 100 others should have a single value.
... ... @@ -92,12 +106,16 @@ An option can be declared with a default or an implicit value, or both.
92 106 A default value is the value that an option takes when it is not specified
93 107 on the command line. The following specifies a default value for an option:
94 108  
95   - cxxopts::value<std::string>()->default_value("value")
  109 +```cpp
  110 +cxxopts::value<std::string>()->default_value("value")
  111 +```
96 112  
97 113 An implicit value is the value that an option takes when it is given on the
98 114 command line without an argument. The following specifies an implicit value:
99 115  
100   - cxxopts::value<std::string>()->implicit_value("implicit")
  116 +```cpp
  117 +cxxopts::value<std::string>()->implicit_value("implicit")
  118 +```
101 119  
102 120 If an option had both, then not specifying it would give the value `"value"`,
103 121 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
134 152 completely replaced by calling `options.custom_help`. Note that you might
135 153 also want to override the positional help by calling `options.positional_help`.
136 154  
  155 +
  156 +## Example
  157 +
  158 +Putting all together:
  159 +```cpp
  160 +int main(int argc, char** argv)
  161 +{
  162 + cxxopts::Options options("test", "A brief description");
  163 +
  164 + options.add_options()
  165 + ("b,bar", "Param bar", cxxopts::value<std::string>())
  166 + ("d,debug", "Enable debugging", cxxopts::value<bool>()->default_value("false"))
  167 + ("f,foo", "Param foo", cxxopts::value<int>()->default_value("10"))
  168 + ("h,help", "Print usage")
  169 + ;
  170 +
  171 + auto result = options.parse(argc, argv);
  172 +
  173 + if (result.count("help"))
  174 + {
  175 + std::cout << options.help() << std::endl;
  176 + exit(0);
  177 + }
  178 + bool debug = result["debug"].as<bool>();
  179 + std::string bar;
  180 + if (result.count("bar"))
  181 + bar = result["bar"].as<std::string>();
  182 + int foo = result["foo"].as<int>();
  183 +
  184 + return 0;
  185 +}
  186 +```
  187 +
137 188 # Linking
138 189  
139 190 This is a header only library.
... ...