Minimalist header-only C++17 command line parsing function
Go to file
Mattéo Delabre 5619ec178c
Add documentation & license block
2020-08-18 18:57:39 +02:00
.gitignore Initial commit 2019-08-08 23:21:17 +02:00
LICENSE Initial commit 2019-08-08 23:21:17 +02:00
Makefile Initial commit 2019-08-08 23:21:17 +02:00
README.md Initial commit 2019-08-08 23:21:17 +02:00
main.cpp Initial commit 2019-08-08 23:21:17 +02:00
options.hpp Add documentation & license block 2020-08-18 18:57:39 +02:00
tests.cpp Initial commit 2019-08-08 23:21:17 +02:00

README.md

Options

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

Background

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 formatthe GNU/Unix formataccording 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.

Goal

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.

Usage

Installation

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

Documentation

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 http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html); the conventions of Chapter 10 in “The Art of Unix Programming” by Eric S. Raymond; and the author's personal habits.

Arguments:

  • start: Iterator to the initial argument.
  • end: Past-the-end iterator to signal the argument lists end.

Return value: Dictionary containing, for each option present on the command line, a key-value pair with the options 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.

Example

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: ";
        }
        else
        {
            std::cerr << '"' << name << "\": ";
        }

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

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

        std::cerr << '\n';
    }
}

Other libraries

License

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