options/README.md

67 lines
4.2 KiB
Markdown
Raw Permalink Normal View History

2019-08-08 17:54:29 +00:00
# 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](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html), specified by the POSIX standard and available on most systems, and [Boost.Program\_options](https://www.boost.org/doc/libs/release/doc/html/program_options.html).
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!](https://github.com/adishavit/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](options.hpp) header into your project and include it where appropriate.
### Documentation
```cpp
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:** A pair with, first: a 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 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.
2019-08-08 17:54:29 +00:00
### Example
[This program](main.cpp) prints all options and operands that are passed to it on the command line.
2019-08-08 17:54:29 +00:00
## Other libraries
* [Argh!](https://github.com/adishavit/argh)
* [args](https://github.com/Taywee/args)
* [Argument\_helper](http://graphics.stanford.edu/~drussel/Argument_helper/)
* [Boost.Program\_options](https://www.boost.org/doc/libs/release/doc/html/program_options.html)
* [CLI](https://www.codesynthesis.com/projects/cli/)
* [CmdLine](http://www.bradapp.com/ftp/src/libs/C++/CmdLine.html)
* [cv::CommandLineParser](https://docs.opencv.org/4.1.1/d0/d2e/classcv_1_1CommandLineParser.html)
* [cxxopts](https://github.com/jarro2783/cxxopts)
* [docopt](https://github.com/docopt/docopt.cpp)
* [getoptpp](https://code.google.com/archive/p/getoptpp/)
* [gflags](https://github.com/gflags/gflags)
* [optionparser](http://optionparser.sourceforge.net/)
* [QCommandLineParser](https://doc.qt.io/qt-5/qcommandlineparser.html)
* [TCLAP](http://tclap.sourceforge.net/)
## License
This library is released into the public domain. See [the license file](LICENSE) for more information.