Mattéo Delabre 087353c487 | ||
---|---|---|
.gitignore | ||
LICENSE | ||
Makefile | ||
README.md | ||
main.cpp | ||
options.hpp | ||
tests.cpp |
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 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.
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 list’s end.
Return value: A pair with, first: a 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 has no associated option-arguments (i.e. is a flag), the value in the dictionary is an empty list; second: a list in which all operands are grouped following the order of the command line.
Example
This program prints all options and operands that are passed to it on the command line.
Other libraries
- Argh!
- args
- Argument_helper
- Boost.Program_options
- CLI
- CmdLine
- cv::CommandLineParser
- cxxopts
- docopt
- getoptpp
- gflags
- optionparser
- QCommandLineParser
- TCLAP
License
This library is released into the public domain. See the license file for more information.