Minimalist header-only C++17 command line parsing function
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Mattéo Delabre aa3e05a917
Initial commit
11 months ago
.gitignore Initial commit 11 months ago
LICENSE Initial commit 11 months ago
Makefile Initial commit 11 months ago Initial commit 11 months ago
main.cpp Initial commit 11 months ago
options.hpp Initial commit 11 months ago
tests.cpp Initial commit 11 months ago


Minimalist header-only C++17 command line parsing function.


A C++ program can access the command line with which it was invoked through the argv argument of the main function, which contains an array of whitespace-separated tokens. Most programs offer a richer interface than a simple list of values separated by whitespace: a common kind of interface consists in providing the user with a set of options that can be toggled or configured. Users expect programs to accept those options in a standard format — the GNU/Unix format — according to which the command line is to be parsed.

Parsing the command line is a common task for which many libraries have been created, most notably getopt, specified by the POSIX standard and available on most systems, and Boost.Program_options. Nearly all of these libraries provide developers with a similar interface: you need to first precisely define the list of options that should be accepted by your program, specifying short and long names, accepted format and multiplicity for each one along with a description, then trigger the parsing and get the result inside a special structure. Those libraries can feel too complex for simple projects and too rigid for quick experimentations, which is what the Argh! library addresses: its philosophy is to simply provide the developers with direct access to the set of options that were passed, without any kind of prior specification.


This library follows the same philosophy as Argh! but with a much lighter implementation, providing developers with a function that directly parses the command line into a dictionary of options in less than 100 lines of code.



Copy the options.hpp header into your project and include it where appropriate.


template<typename Iterator>
auto options::parse(Iterator start, Iterator end)

Synopsis: Parse a list of command line arguments into a set of program options.

This implementation strives to follow (in decreasing precedence) the guidelines laid out in Chapter 12 of the POSIX.1-2017 specification (see; the conventions of Chapter 10 in “The Art of Unix Programming” by Eric S. Raymond; and the author’s personal habits.


  • start: Iterator to the initial argument.
  • end: Past-the-end iterator to signal the argument list’s end.

Return value: Dictionary containing, for each option present on the command line, a key-value pair with the option’s name as the key and the list of option-arguments associated to that option, following the order of the command line, as the value. If an option is has no associated option-arguments (i.e. is a flag), the value in the dictionary is an empty list. Operands are grouped in a special item using options::operands as its key and also follow the order of the command line.


This simple program prints all options that are passed to it on the command line.

#include "options.hpp"
#include <iostream>

int main(int argc, char** argv)
    auto opts = options::parse(argv + 1, argv + argc);

    for (const auto& [name, args] : opts)
        if (name == options::operands)
            std::cerr << "Operands: ";
            std::cerr << '"' << name << "\": ";

        if (args.empty())
            std::cerr << "(no arguments)";
            for (
                auto value_it = std::cbegin(args);
                value_it != std::cend(args);
                std::cerr << '"' << *value_it << '"';

                if (std::next(value_it) != std::cend(args))
                    std::cerr << ", ";

        std::cerr << '\n';

Other libraries


This library is released into the public domain. See the license file for more information.