Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/jarro2783/cxxopts.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomas Barton <barton.tomas@gmail.com>2020-02-14 00:02:27 +0300
committerGitHub <noreply@github.com>2020-02-14 00:02:27 +0300
commitb0f67a06de3446aa97a4943ad0ad6086460b2b61 (patch)
treeeb2f418c207268ef0d6ad5dda9018c4022fcae12
parent6fa46a748838d5544ff8e9ab058906ba2c4bc0f3 (diff)
Code highlighting, add more examples (#220)
-rw-r--r--README.md75
1 files 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 <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.