2016-08-25 11:36:32 +03:00
|
|
|
[![Build Status](https://travis-ci.org/jarro2783/cxxopts.svg?branch=master)](https://travis-ci.org/jarro2783/cxxopts)
|
|
|
|
|
2015-09-28 05:54:46 +03:00
|
|
|
# Quick start
|
2014-10-13 02:20:10 +04:00
|
|
|
|
2014-10-03 09:59:03 +04:00
|
|
|
This is a lightweight C++ option parser library, supporting the standard GNU
|
|
|
|
style syntax for options.
|
|
|
|
|
|
|
|
Options can be given as:
|
|
|
|
|
|
|
|
--long
|
|
|
|
--long=argument
|
|
|
|
--long argument
|
|
|
|
-a
|
|
|
|
-ab
|
|
|
|
-abc argument
|
|
|
|
|
|
|
|
where c takes an argument, but a and b do not.
|
|
|
|
|
2016-01-22 09:46:22 +03:00
|
|
|
Additionally, anything after `--` will be parsed as a positional argument.
|
|
|
|
|
2015-09-28 05:56:03 +03:00
|
|
|
## Basics
|
2014-10-03 09:59:03 +04:00
|
|
|
|
|
|
|
#include <cxxopts.hpp>
|
|
|
|
|
|
|
|
Create a cxxopts::Options instance.
|
|
|
|
|
2016-05-13 01:06:13 +03:00
|
|
|
cxxopts::Options options("MyProgram", "One line description of MyProgram");
|
2014-10-03 09:59:03 +04:00
|
|
|
|
2016-01-27 23:26:10 +03:00
|
|
|
Then use `add_options`.
|
2014-10-03 09:59:03 +04:00
|
|
|
|
|
|
|
options.add_options()
|
|
|
|
("d,debug", "Enable debugging")
|
|
|
|
("f,file", "File name", cxxopts::value<std::string>())
|
2016-05-13 01:06:13 +03:00
|
|
|
;
|
2014-10-03 09:59:03 +04:00
|
|
|
|
2017-01-29 23:28:57 +03:00
|
|
|
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.
|
|
|
|
Any type can be given as long as it can be parsed, with operator>>.
|
2014-10-03 09:59:03 +04:00
|
|
|
|
|
|
|
To parse the command line do:
|
|
|
|
|
|
|
|
options.parse(argc, argv);
|
|
|
|
|
2015-09-28 05:56:03 +03:00
|
|
|
To retrieve an option use `options.count("option")` to get the number of times
|
2014-10-03 09:59:03 +04:00
|
|
|
it appeared, and
|
|
|
|
|
|
|
|
options["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.
|
|
|
|
|
2016-01-27 23:26:10 +03:00
|
|
|
## Help groups
|
|
|
|
|
|
|
|
Options can be placed into groups for the purposes of displaying help messages.
|
|
|
|
To place options in a group, pass the group as a string to `add_options`. Then,
|
|
|
|
when displaying the help, pass the groups that you would like displayed as a
|
|
|
|
vector to the `help` function.
|
|
|
|
|
2015-09-28 05:54:46 +03:00
|
|
|
## Positional Arguments
|
2015-09-28 05:51:30 +03:00
|
|
|
|
2016-01-27 23:26:10 +03:00
|
|
|
Positional arguments can be optionally parsed into one or more options.
|
|
|
|
To set up positional arguments, call
|
2015-09-28 05:51:30 +03:00
|
|
|
|
|
|
|
options.parse_positional({"first", "second", "last"})
|
|
|
|
|
2016-01-27 23:26:10 +03:00
|
|
|
where "last" should be the name of an option with a container type, and the
|
|
|
|
others should have a single value.
|
2015-09-28 05:51:30 +03:00
|
|
|
|
2016-08-25 01:35:01 +03:00
|
|
|
## Default and implicit values
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
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"`,
|
|
|
|
and writing `--option=another` would give it the value `"another"`.
|
|
|
|
|
2015-09-28 05:54:46 +03:00
|
|
|
# Linking
|
2014-10-13 02:20:10 +04:00
|
|
|
|
2014-10-15 15:17:31 +04:00
|
|
|
This is a header only library.
|
2014-10-13 02:20:10 +04:00
|
|
|
|
2015-09-28 05:54:46 +03:00
|
|
|
# Requirements
|
2014-10-13 02:20:10 +04:00
|
|
|
|
2016-01-22 09:46:22 +03:00
|
|
|
The only build requirement is a C++ compiler that supports C++11 regular
|
2014-10-13 02:20:10 +04:00
|
|
|
expressions. For example GCC >= 4.9 or clang with libc++.
|
|
|
|
|
2014-10-03 09:59:03 +04:00
|
|
|
|
2015-09-28 05:54:46 +03:00
|
|
|
# TODO list
|
2014-10-03 09:59:03 +04:00
|
|
|
|
|
|
|
* Allow unrecognised options.
|
2014-10-15 15:16:37 +04:00
|
|
|
* Various help strings.
|
2014-10-21 02:27:24 +04:00
|
|
|
* Unicode aware for help strings.
|