cxxopts

GitHub

Lightweight C++ command line option parser

RAW Doc

CHANGELOG

Changelog

This is the changelog for cxxopts, a C++11 library for parsing command line
options. The project adheres to semantic versioning.

3.3.1

Bug fixes

* Added missing version bump

3.3.0

Bug fixes

* Added missing header for GCC 15
* Fix some CMake packaging problems

Added

* as_optional
* ParseResult::contains
* std::filesystem support
* CMake 4 compatibility

3.2.1

Bug fixes

* Fix compilation with optional on C++20.

3.2

Bug fixes

* Fix unannotated fallthrough.
* Fix sign conversion with Unicode output.
* Don't initialize regex in static initialiser.
* Fix incorrect integer overflow checks.

Added

* Add fuzzing to CI

Changed

* Change quote output to '' to match Windows.
* Don't split positional arguments by the list delimiter.
* Order help groups by the order they were added.

3.1.1

Bug Fixes

* Fixed version number in header.
* Fixed cast warning in Unicode function.

3.1

Added

* Support for multiple long names for the same option (= multiple long aliases)
* Add a program() function to retrieve the program name.
* Added a .clang-format file.
* Added iterator and printing for a ParseResult.

Changed

* Cleanup exception code, add cxxopts::exceptions namespace.
* Renamed several exceptions to be more descriptive, and added to a nested namespace.

Bug Fixes

* Fix arguments() having no key for options that only have a short name.

3.0

Changed

* Only search for a C++ compiler in CMakeLists.txt.
* Allow for exceptions to be disabled.
* Fix duplicate default options when there is a short and long option.
* Add CXXOPTS_NO_EXCEPTIONS to disable exceptions.
* Fix char parsing for space and check for length.
* Change argument type in Options::parse from char to const char.
* Refactor parser to not change its arguments.
* ParseResult doesn't depend on a reference to the parser.
* Fixed several warnings and code quality issues.
* Improved formatting for help descriptions.
* Improve integer parsing.

Added

* A list of unmatched arguments is available in ParseResult.
* Support single letter options with argument attached.
* Use <optional> if it is present.

Bug Fixes

* Fix missing option name in exception.

2.2

Changed

* Allow integers to have leading zeroes.
* Build the tests by default.
* Don't check for container when showing positional help.

Added

* Iterator inputs to parse_positional.
* Throw an exception if the option in parse_positional doesn't exist.
* Parse a delimited list in a single argument for vector options.
* Add an option to disable implicit value on booleans.

Bug Fixes

* Fix a warning about possible loss of data.
* Fix version numbering in CMakeLists.txt
* Remove unused declaration of the undefined ParseResult::get_option.
* Throw on invalid option syntax when beginning with a -.
* Throw in as when option wasn't present.
* Fix catching exceptions by reference.
* Fix out of bounds errors parsing integers.

2.1.1

Bug Fixes

* Revert the change adding const type for argv, because most users expect
to pass a non-const argv from main.

2.1

Changed

* Options with implicit arguments now require the --option=value form if
they are to be specified with an option. This is to remove the ambiguity
when a positional argument could follow an option with an implicit value.
For example, --foo value, where foo has an implicit value, will be
parsed as --foo=implicit and a positional argument value.
* Boolean values are no longer special, but are just an option with a default
and implicit value.

Added

* Added support for std::optional as a storage type.
* Allow the help string to be customised.
* Use const for the type in the argv parameter, since the contents of the
arguments is never modified.

Bug Fixes

* Building against GCC 4.9 was broken due to overly strict shadow warnings.
* Fixed an ambiguous overload in the parse_positional function when an
initializer_list was directly passed.
* Fixed precedence in the Boolean value regex.

2.0

Changed

* Options::parse returns a ParseResult rather than storing the parse
result internally.
* Options with default values now get counted as appearing once if they
were not specified by the user.

Added

* A new ParseResult object that is the immutable result of parsing. It
responds to the same count and operator[] as Options of 1.x did.
* The function ParseResult::arguments returns a vector of the parsed
arguments to iterate through in the order they were provided.
* The symbol cxxopts::version for the version of the library.
* Booleans can be specified with various strings and explicitly set false.

1.x

The 1.x series was the first major version of the library, with release numbers
starting to follow semantic versioning, after 0.x being unstable. It never had
a changelog maintained for it. Releases mostly contained bug fixes, with the
occasional feature added.

---

README

<div align="center">

[](https://github.com/jarro2783/cxxopts/actions/workflows/cmake.yml)
[](https://conan.io/center/recipes/cxxopts)
[](https://vcpkg.io/en/package/cxxopts)
[](https://github.com/jarro2783/cxxopts/releases)

[](https://github.com/jarro2783/cxxopts)
[](https://github.com/fffaraz/awesome-cpp)
[](https://formulae.brew.sh/formula/cxxopts)

[](./LICENSE)

</div>

Release versions

Note that master is generally a work in progress, and you probably want to use a
tagged release version.

Version 3 breaking changes

If you have used version 2, there are a couple of breaking changes in version 3
that you should be aware of. If you are new to cxxopts you can skip this
section.

The parser no longer modifies its arguments, so you can pass a const argc and
argv and expect them not to be changed.

The ParseResult object no longer depends on the parser. So it can be returned
from a scope outside the parser and still work. Now that the inputs are not
modified, ParseResult stores a list of the unmatched arguments. These are
retrieved like follows:

cpp
auto result = options.parse(argc, argv);
result.unmatched(); // get the unmatched arguments

Quick start

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.

Additionally, anything after -- will be parsed as a positional argument.

Basics

cpp
#include <cxxopts.hpp>

Create a cxxopts::Options instance.

cpp
cxxopts::Options options("MyProgram", "One line description of MyProgram");

Then use add_options.

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.
Any type can be given as long as it can be parsed, with operator>>.

To parse the command line do:

cpp
auto result = options.parse(argc, argv);

To retrieve an option use result.count("option") to get the number of times
it appeared, and

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.

Unrecognised arguments

You can allow unrecognised arguments to be skipped. This applies to both
positional arguments that are not parsed into another option, and --
arguments that do not match an argument that you specify. This is done by
calling:

cpp
options.allow_unrecognised_options();

and in the result object they are retrieved with:

cpp
result.unmatched()

Exceptions

Exceptional situations throw C++ exceptions. There are two types of
exceptions: errors defining the options, and errors when parsing a list of
arguments. All exceptions derive from cxxopts::exceptions::exception. Errors
defining options derive from cxxopts::exceptions::specification and errors
parsing arguments derive from cxxopts::exceptions::parsing.

All exceptions define a what() function to get a printable string
explaining the error.

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, help by default prints help for all groups. If you want to only print help for a specific group, pass the groups that you would like displayed as a vector to the help function.

Positional Arguments

Positional arguments are those given without a preceding flag and can be used
alongside non-positional arguments. There may be multiple positional arguments,
and the final positional argument may be a container type to hold a list of all
remaining positionals.

To set up positional arguments, first declare the options, then configure a
set of those arguments as positional like:

cpp
options.add_options()
("script", "The script file to execute", cxxopts::value<std::string>())
("server", "The server to execute on", cxxopts::value<std::string>())
("filenames", "The filename(s) to process", cxxopts::value<std::vector<std::string>>());

options.parse_positional({"script", "server", "filenames"});

// Parse options the usual way
options.parse(argc, argv);

Note : parse_positional defaults to replacing the positionals with the arguments provided in the function call. To append to the existing list of positionals, please use cxxopts::PositionalMode::Append.

cpp
options.parse_positional("another_option", cxxopts::PositionalMode::Append);

For example, parsing the following arguments:
~~~
my_script.py my_server.com file1.txt file2.txt file3.txt
~~~
will result in parsed arguments like the following table:

| Field | Value |
| ------------- | ----------------------------------------- |
| "script" | "my_script.py" |
| "server" | "my_server.com" |
| "filenames" | {"file1.txt", "file2.txt", "file3.txt"} |

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:

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:

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",
and writing --option=another would give it the value "another".

Note that if option has an implicit value, specifying --option another will not work. You must use = syntax - --option=another or -o=another(assumming o is a short name for the option). This is because no argument is required. Hence, there is no good way to determine if the next string is an argument to your option or a positional argument.

Note that the default and implicit value is always stored as a string,
regardless of the type that you want to store it in. It will be parsed as
though it was given on the command line.

Default values are not counted by Options::count.

Implicit values with disabled arguments

You can specify an option for which an argument cannot be specified using below syntax.

cpp
cxxopts::value<bool>()->implicit_value("true", cxxopts::ImplicitArgPolicy::Disabled)

In this case specifying option as --option=<value> is disallowed and will throw specified_disabled_args exception. Note that --option value is not supported for implicit values. It is treated as positional/unmatched.

Boolean values

Boolean options have a default implicit value of "true", which can be
overridden. The effect is that writing -o by itself will set option o to
true. However, they can also be written with various strings using =value.
There is no way to disambiguate positional arguments from the value following
a boolean, so we have chosen that they will be positional arguments, and
therefore, -o false does not work.

std::vector<T> values

Parsing a list of values into a std::vector<T> is also supported, as long as T
can be parsed. To separate single values in a list the define symbol CXXOPTS_VECTOR_DELIMITER
is used, which is ',' by default. Ensure that you use no whitespaces between values
(unless the entire list is double quoted) because those would be interpreted as the
next command line option. Example for a command line option
that can be parsed as a std::vector<double>:

~~~
--my_list=1,-2.1,3,4.5
~~~

If the list of values is quoted, then spaces can be included. For example:

~~~
--my_list="review,memory sanitize,build help,reformat"
~~~

This will be parsed into review, memory sanitize, build help, and reformat.

Options specified multiple times

The same option can be specified several times, with different arguments, which will all
be recorded in order of appearance. An example:

~~~
--use train --use bus --use ferry
~~~

this is supported through the use of a vector of value for the option:

~~~
options.add_options()
("use", "Usable means of transport", cxxopts::value<std::vector<std::string>>())
~~~

Custom help

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
#include <iostream>
#include "cxxopts.hpp"

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.

Requirements

The only build requirement is a C++ compiler that supports C++11 features such as:

* regex
* constexpr
* default constructors

GCC >= 4.9 or clang >= 3.1 with libc++ are known to work.

The following compilers are known not to work:

* MSVC 2013

---