adishavit / argh

Argh! A minimalist argument handler.
BSD 3-Clause "New" or "Revised" License
1.33k stars 93 forks source link
argument-parser cli cli-args command-line command-line-parser cpp11 getopt getopts header-only single-file

logo

Frustration-free command line processing

Language Standard License Try it online Build Status

So many different command line processing libraries out there and none of them just work!
Some bring their whole extended family of related and unrelated external dependencies (yes, I'm looking at you Boost).
Some require quirky syntax and/or very verbose setups that sacrifice simplicity for the generation of a cute usage message and validation. Many come to dominate your main() file and yet others do not build on multiple plaforms - for some even their own tests and trivial usage cause crashes on some systems. Argh!

If you're writing a highly-sophisticated command line tool, then Boost.Program_options and its kind might give you many advanced options. However, if you need to get up and running quickly, effectively and with minimal fuss, give the single header-file argh a try.

TL;DR

It doesn't get much simpler than this:

#include <iostream>
#include "argh.h"

int main(int, char* argv[])
{
    argh::parser cmdl(argv);

    if (cmdl[{ "-v", "--verbose" }])
        std::cout << "Verbose, I am.\n";

    return EXIT_SUCCESS;
}

TL;DR Videos

Philosophy

Contrary to many alternatives, argh takes a minimalist laissez-faire approach, very suitable for fuss-less prototyping with the following rules:

The API is:

argh does not care about:

Tutorial

Create parser:

auto cmdl = argh::parser(argc, argv);

In fact, you can even drop argc. This will also work:

argh::parser cmdl(argv);

Positional argument access by (integer) index with [<size_t>]:

cout << "Exe name is: " << cmdl[0] << '\n';
                               ^^^
assert(cmdl[10000].empty()); // out-of-bound index returns empty string
            ^^^^^

Boolean flag argument access by (string) name with [<std::string>]:

cout << "Verbose mode is " << ( cmdl["verbose"] ? "ON" : "OFF" ) << '\n';
                                    ^^^^^^^^^^^

Any dashes are trimmed so are not required.

Your flag can have several alternatives, just list them with [{ "<name-1>", "<name-2>", ... }]:

cout << "Verbose mode is " << ( cmdl[{ "-v", "--verbose" }] ? "ON" : "OFF" ) << '\n';
                                    ^^^^^^^^^^^^^^^^^^^^^^^

Beyond bool and std::string access with [], as shown above, we can also access the argument values as an std::istream. This is very useful for type conversions.

std::istream positional argument access by (integer) index with (<size_t>):

std::string my_app_name;
cmdl(0) >> my_app_name; // streaming into a string
    ^^^
cout << "Exe name is: " << my_app_name << '\n';

We can also check if a particular positional arg was given or not (this is like using [<std::string>] above):

if (!cmdl(10))
  cerr << "Must provide at least 10 arguments!" << '\n';
else if (cmdl(11))
  cout << "11th argument  is: " << cmdl[11] << '\n';

But we can also set default values for positional arguments. These are passed as the second argument:

float scale_factor;
cmdl(2, 1.0f) >> scale_factor;
     ^^^^^^^

If the position argument was not given or the streaming conversion failed, the default value will be used.

Similarly, parameters can be accessed by name(s) (i.e. by string or list of string literals) with:
(<std::string> [, <default value>]) or ({ "<name-1>", "<name-2>", ... } [, <default value>]):

float scale_factor;
cmdl("scale", 1.0f) >> scale_factor; // Use 1.0f as default value
     ^^^^^^^^^^^^^

float threshold;
if (!(cmdl({ "-t", "--threshold"}) >> threshold)) // Check for missing param and/or bad (inconvertible) param value
  cerr << "Must provide a valid threshold value! Got '" << cmdl("threshold").str() << "'" << endl;
else                                                                        ^^^^^^
  cout << "Threshold set to: " << threshold << '\n';

As shown above, use std::istream::str() to get the param value as a std:string or just stream the value into a variable of a suitable type. Standard stream state indicates failure, including when the argument was not given.
When using multiple names, the first value found will be returned.

Positional arguments can be iterated upon directly using range-for:

cout << "Positional args:\n";
for (auto& pos_arg : cmdl)
  cout << '\t' << pos_arg << '\n';

Similarly, cmdl.size() will return the count of positional arguments.

Positional arguments, flags and parameters are accessible as "ranges":

cout << "Positional args:\n";
for (auto& pos_arg : cmdl.pos_args())
  cout << '\t' << pos_arg << '\n';

cout << "\nFlags:\n";
for (auto& flag : cmdl.flags())
  cout << '\t' << flag << '\n';

cout << "\nParameters:\n";
for (auto& param : cmdl.params())
  cout << '\t' << param.first << " : " << param.second << '\n';

If a parameter appears several times in the command line, all its duplicates may be accessed, in order, like so:

cout << "\nValues for all `--input` parameters:\n";
for (auto& param : cmdl.params("input"))  // iterate on all params called "input"
  cout << '\t' << param.first << " : " << param.second << '\n';

By default, options are assumed to be boolean flags. When this is not what you want, there are several ways to specify when an option is a parameter with an associated value.

  1. Specify PREFER_PARAM_FOR_UNREG_OPTION mode to interpret any <option> <non-option> as <parameter-name> <parameter-value>:

    using namespace argh;
    auto cmdl = parser(argc, argv, parser::PREFER_PARAM_FOR_UNREG_OPTION);
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cout << cmdl("--threshold").str() << '\n';
  2. Pre-register an expected parameter name with add_param() (before calling parse()):

    argh::parser cmdl;
    cmdl.add_param("threshold"); // pre-register "threshold" as a param: name + value
    cmdl.parse(argc, argv);
    cout << cmdl("threshold").str() << '\n';

    You may also batch pre-register multiple options as parameters with add_params({ ... }):

    argh::parser cmdl;
    cmdl.add_params({ "-t", "--threshold", "-s", "--scale" }); // batch pre-register multiple params: name + value
    cmdl.parse(argc, argv);
    cout << cmdl("threshold").str() << '\n';

    Unregistered options will default to boolean flags.

  3. Since pre-registration has to be done before parsing, we might as well just use the ctor:

    argh::parser cmdl({ "-t", "--threshold", "-s", "--scale" }); // batch pre-register multiple params: name + value
    cmdl.parse(argc, argv);
    cout << cmdl("threshold").str() << '\n';
  4. Use a = with no spaces around it within the option when calling the app:

    >> my_app --threshold=42
    42

    This will automatically be interpreted as a named parameter-value pair.

Tips

Terminology

Any command line is composed of 2 types of Args:

  1. Positional Args:
    Free standing, in-order, values
    e.g. config.json
  2. Options:
    Args beginning with - (and that are not negative numbers).
    We identify 2 kinds of Options:
    1. Flags:
      Boolean options => (appear ? true : false)
      e.g. -v, --verbose
    2. Parameters:
      A named value followed by a non-option value
      e.g. --gamma 2.2

Thus, any command line can always be broken into some combination of (1) positional args (2) flags and (3) parameters.

API Summary

Parsing

Parse the command line using either

Special Parsing Modes

Extra flexibility can be added by specifying parsing modes:

Argument Access

The streaming happens at the user's side, so conversion failure can be checked there: e.g

if (!(cmdl("scale") >> scale_factor))
  cerr << "Must provide valid scale factor!" << '\n';

Use the .str() method to get the parameter value as a string: e.g. cmdl("name").str();

More Methods

Finding Argh!

Finding Argh! - CMake

The provided CMakeLists.txt generates targets for tests, a demo application and an install target to install argh system-wide and make it known to CMake. You can control generation of test and example targets using the options BUILD_TESTS and BUILD_EXAMPLES. Only argh alongside its license and readme will be installed - not tests and demo!

Add argh to your CMake-project by using

find_package(argh)

The package exports argh INTERFACE library target and argh_INCLUDE_DIR variable. Make argh.h known to your compiler by using one of the following methods; both will make the location of argh.h known to the compiler, not link in a precompiled library - even when using target_link_libraries().

target_include_directories(${MY_TARGET_NAME} PRIVATE "${argh_INCLUDE_DIR}")
#OR
target_link_libraries(${MY_TARGET_NAME} argh)

Additional Build Systems

Buck
[Buck](https://buckbuild.com/) support: Run the example: ```bash= buck run :example ``` Run the tests: ```bash= buck run :tests buck run test_package ``` If you take `argh` as a submodule, then the visible target is `//:argh`.

Colophon

I ❤ your feedback.
If you found Argh! useful - do Tweet about it to let me know.
If you found it lacking, please post an issue.