Skip to content

Quick start

idubinov edited this page Aug 26, 2019 · 5 revisions

Getting started

First, you only need to include:

#include <cxxopts.hpp>

Then, make an instance of cxxopts::Options:

cxxopts::Options options;

Adding options

You can then add options like so:

bool compile = false;

options.add_options()
  ("f,file", "File", cxxopts::value<std::string>())
  ("c,compile", "Compile", cxxopts::value(compile));

The options can be specified with a short option, which is a single character, or a long option, which is two or more characters, or both, separated by a comma; the short option must come first.

Parsing arguments

To parse the command line arguments, simple write:

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

This will parse the arguments and modify argc and argv, removing all recognised arguments.

Getting values

To get the number of times that an option appeared on the command line, use:

result.count("option");

To retrieve the parsed value, it can be accessed with operator[]. The value returned is a generic value which must be converted to the type of the value stored. For example, for the option "file" above, whose value is a string, it must be converted to a string using:

result["file"].as<std::string>();
Clone this wiki locally