README.md
1[](https://travis-ci.org/jarro2783/cxxopts)
2
3# Release versions
4
5Note that `master` is generally a work in progress, and you probably want to use a
6tagged release version.
7
8## Version 3 breaking changes
9
10If you have used version 2, there are a couple of breaking changes in (the as
11yet unreleased, current master) version 3 that you should be aware of. If you are new to
12`cxxopts` you can skip this section.
13
14The parser no longer modifies its arguments, so you can pass a const `argc` and
15`argv` and expect them not to be changed.
16
17The `ParseResult` object no longer depends on the parser. So it can be returned
18from a scope outside the parser and still work. Now that the inputs are not
19modified, `ParseResult` stores a list of the unmatched arguments. These are
20retrieved like follows:
21
22```cpp
23auto result = options.parse(argc, argv);
24result.unmatched(); // get the unmatched arguments
25```
26
27# Quick start
28
29This is a lightweight C++ option parser library, supporting the standard GNU
30style syntax for options.
31
32Options can be given as:
33
34 --long
35 --long=argument
36 --long argument
37 -a
38 -ab
39 -abc argument
40
41where c takes an argument, but a and b do not.
42
43Additionally, anything after `--` will be parsed as a positional argument.
44
45## Basics
46
47```cpp
48#include <cxxopts.hpp>
49```
50
51Create a `cxxopts::Options` instance.
52
53```cpp
54cxxopts::Options options("MyProgram", "One line description of MyProgram");
55```
56
57Then use `add_options`.
58
59```cpp
60options.add_options()
61 ("d,debug", "Enable debugging") // a bool parameter
62 ("i,integer", "Int param", cxxopts::value<int>())
63 ("f,file", "File name", cxxopts::value<std::string>())
64 ("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false"))
65 ;
66```
67
68Options are declared with a long and an optional short option. A description
69must be provided. The third argument is the value, if omitted it is boolean.
70Any type can be given as long as it can be parsed, with operator>>.
71
72To parse the command line do:
73
74```cpp
75auto result = options.parse(argc, argv);
76```
77
78To retrieve an option use `result.count("option")` to get the number of times
79it appeared, and
80
81```cpp
82result["opt"].as<type>()
83```
84
85to get its value. If "opt" doesn't exist, or isn't of the right type, then an
86exception will be thrown.
87
88Note that the result of `options.parse` should only be used as long as the
89`options` object that created it is in scope.
90
91## Unrecognised arguments
92
93You can allow unrecognised arguments to be skipped. This applies to both
94positional arguments that are not parsed into another option, and `--`
95arguments that do not match an argument that you specify. This is done by
96calling:
97
98```cpp
99options.allow_unrecognised_options();
100```
101
102and in the result object they are retrieved with:
103
104```cpp
105result.unmatched()
106```
107
108## Exceptions
109
110Exceptional situations throw C++ exceptions. There are two types of
111exceptions: errors defining the options, and errors when parsing a list of
112arguments. All exceptions derive from `cxxopts::OptionException`. Errors
113defining options derive from `cxxopts::OptionSpecException` and errors
114parsing arguments derive from `cxxopts::OptionParseException`.
115
116All exceptions define a `what()` function to get a printable string
117explaining the error.
118
119## Help groups
120
121Options can be placed into groups for the purposes of displaying help messages.
122To place options in a group, pass the group as a string to `add_options`. Then,
123when displaying the help, pass the groups that you would like displayed as a
124vector to the `help` function.
125
126## Positional Arguments
127
128Positional arguments can be optionally parsed into one or more options.
129To set up positional arguments, call
130
131```cpp
132options.parse_positional({"first", "second", "last"})
133```
134
135where "last" should be the name of an option with a container type, and the
136others should have a single value.
137
138## Default and implicit values
139
140An option can be declared with a default or an implicit value, or both.
141
142A default value is the value that an option takes when it is not specified
143on the command line. The following specifies a default value for an option:
144
145```cpp
146cxxopts::value<std::string>()->default_value("value")
147```
148
149An implicit value is the value that an option takes when it is given on the
150command line without an argument. The following specifies an implicit value:
151
152```cpp
153cxxopts::value<std::string>()->implicit_value("implicit")
154```
155
156If an option had both, then not specifying it would give the value `"value"`,
157writing it on the command line as `--option` would give the value `"implicit"`,
158and writing `--option=another` would give it the value `"another"`.
159
160Note that the default and implicit value is always stored as a string,
161regardless of the type that you want to store it in. It will be parsed as
162though it was given on the command line.
163
164## Boolean values
165
166Boolean options have a default implicit value of `"true"`, which can be
167overridden. The effect is that writing `-o` by itself will set option `o` to
168`true`. However, they can also be written with various strings using `=value`.
169There is no way to disambiguate positional arguments from the value following
170a boolean, so we have chosen that they will be positional arguments, and
171therefore, `-o false` does not work.
172
173## `std::vector<T>` values
174
175Parsing of list of values in form of an `std::vector<T>` is also supported, as long as `T`
176can be parsed. To separate single values in a list the definition `CXXOPTS_VECTOR_DELIMITER`
177is used, which is ',' by default. Ensure that you use no whitespaces between values because
178those would be interpreted as the next command line option. Example for a command line option
179that can be parsed as a `std::vector<double>`:
180
181~~~
182--my_list=1,-2.1,3,4.5
183~~~
184
185## Options specified multiple times
186
187The same option can be specified several times, with different arguments, which will all
188be recorded in order of appearance. An example:
189
190~~~
191--use train --use bus --use ferry
192~~~
193
194this is supported through the use of a vector of value for the option:
195
196~~~
197options.add_options()
198 ("use", "Usable means of transport", cxxopts::value<std::vector<std::string>>())
199~~~
200
201## Custom help
202
203The string after the program name on the first line of the help can be
204completely replaced by calling `options.custom_help`. Note that you might
205also want to override the positional help by calling `options.positional_help`.
206
207
208## Example
209
210Putting all together:
211```cpp
212int main(int argc, char** argv)
213{
214 cxxopts::Options options("test", "A brief description");
215
216 options.add_options()
217 ("b,bar", "Param bar", cxxopts::value<std::string>())
218 ("d,debug", "Enable debugging", cxxopts::value<bool>()->default_value("false"))
219 ("f,foo", "Param foo", cxxopts::value<int>()->default_value("10"))
220 ("h,help", "Print usage")
221 ;
222
223 auto result = options.parse(argc, argv);
224
225 if (result.count("help"))
226 {
227 std::cout << options.help() << std::endl;
228 exit(0);
229 }
230 bool debug = result["debug"].as<bool>();
231 std::string bar;
232 if (result.count("bar"))
233 bar = result["bar"].as<std::string>();
234 int foo = result["foo"].as<int>();
235
236 return 0;
237}
238```
239
240# Linking
241
242This is a header only library.
243
244# Requirements
245
246The only build requirement is a C++ compiler that supports C++11 features such as:
247
248* regex
249* constexpr
250* default constructors
251
252GCC >= 4.9 or clang >= 3.1 with libc++ are known to work.
253
254The following compilers are known not to work:
255
256* MSVC 2013
257
258# TODO list
259
260* Allow unrecognised options.
261