Code highlighting, add more examples (#220)
This commit is contained in:
parent
6fa46a7488
commit
b0f67a06de
75
README.md
75
README.md
@ -25,18 +25,26 @@ Additionally, anything after `--` will be parsed as a positional argument.
|
||||
|
||||
## Basics
|
||||
|
||||
#include <cxxopts.hpp>
|
||||
```cpp
|
||||
#include <cxxopts.hpp>
|
||||
```
|
||||
|
||||
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<std::string>())
|
||||
;
|
||||
```cpp
|
||||
options.add_options()
|
||||
("d,debug", "Enable debugging") // a bool parameter
|
||||
("i,integer", "Int param", cxxopts::value<int>())
|
||||
("f,file", "File name", cxxopts::value<std::string>())
|
||||
("v,verbose", "Verbose output", cxxopts::value<bool>()->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<type>()
|
||||
```cpp
|
||||
result["opt"].as<type>()
|
||||
```
|
||||
|
||||
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<std::string>()->default_value("value")
|
||||
```cpp
|
||||
cxxopts::value<std::string>()->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<std::string>()->implicit_value("implicit")
|
||||
```cpp
|
||||
cxxopts::value<std::string>()->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<std::string>())
|
||||
("d,debug", "Enable debugging", cxxopts::value<bool>()->default_value("false"))
|
||||
("f,foo", "Param foo", cxxopts::value<int>()->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<bool>();
|
||||
std::string bar;
|
||||
if (result.count("bar"))
|
||||
bar = result["bar"].as<std::string>();
|
||||
int foo = result["foo"].as<int>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
# Linking
|
||||
|
||||
This is a header only library.
|
||||
|
Loading…
Reference in New Issue
Block a user