C++ Command Line Parser Toolkit
Branch | MSVC 2015, 2017, 2019, 2022 / CLANG 6, 9-17 / GCC 7-13 | Test Coverage |
---|---|---|
master | ||
dev |
C++ command line parser toolkit for kids of all ages.
<filesystem>
support).Check out the complete documentation, you'll be glad you did! With many examples and reference.
#include "dimcli/cli.h"
#include <iostream>
using namespace std;
int main(int argc, char * argv[]) {
Dim::Cli cli;
// Define option that populates an existing variable.
int count;
cli.opt(&count, "c n count", 1).desc("Times to say hello.");
// Or, define option without referencing an existing variable. The variable
// to populate is then implicitly allocated and the returned object is used
// like a smart pointer to access it.
auto & name = cli.opt<string>("name", "Unknown")
.desc("Who to greet.");
// Parse command line.
if (!cli.parse(argc, argv))
return cli.printError(cerr);
// Access the options.
if (!name)
cout << "Greeting the unknown." << endl;
for (int i = 0; i < count; ++i)
cout << "Hello " << *name << "!" << endl;
return 0;
}
What it does when run:
$ a.out -x
Error: Unknown option: -x
$ a.out --help
Usage: a.out [OPTIONS]
Options:
-c, -n, --count=NUM Times to say hello. (default: 1)
--name=STRING Who to greet. (default: Unknown)
--help Show this message and exit.
$ a.out --count=2
Greeting the unknown.
Hello Unknown!
Hello Unknown!
$ a.out --name John
Hello John!
All you need is:
Get the latest dimcli release.
Build it (this example uses Visual C++ 2015 to install a 64-bit build to c:\dimcli on a windows machine):
Why not a single header file?
Sources of inspiration:
Things that were harder than expected:
Other interesting C++ command line parsers: