-
Notifications
You must be signed in to change notification settings - Fork 1
Clap
Dan Parnham edited this page May 31, 2016
·
1 revision
A basic command-line argument parser with a fluent interface.
- Supports short (-h) and long (--help) option names.
- Handles positional arguments.
- Generates formatted usage text.
- Binds to variables for value retrieval (bool, string and numeric types).
#include <emergent/Clap.hpp>
using namespace emergent;
int main(int argc, char *argv[])
{
bool help = false;
long iterations = 100;
string mode = "normal";
string path = "";
Clap clap;
clap['h'].Name("help") .Describe("display this help information") .Bind(help);
clap['i'] .Describe("numeric value without a long name") .Bind(iterations);
clap['m'].Name("mode") .Describe("simple string bind") .Bind(mode);
clap[1] .Name("path") .Describe("first positional argument") .Bind(path);
// Invoke the parser
clap.Parse(argc, argv);
if (help)
{
// Print usage text to std::cout
clap.Usage(cout, argv[0]);
return 0;
}
// ...
return 0;
}
which will display the following usage text
usage: process-name [options] <path>
<path> first positional argument
-h, --help display this help information
-i <value> numeric value without a long name
-m, --mode=<value> simple string bind
If an option is referred to multiple times in a single command-line then only the final one will apply; however if you bind
to a std::vector<>
then every value for that option type will be stored in order. Additionally you can bind to a function
that accepts a string value.
Clap will throw exceptions under the following conditions:
- Unknown option.
- Expected value missing for a non-flag option.
- Incorrect value type (for example, attempting to pass a string to a numeric option).
- An option that has not been bound to a variable will except when used.
- Duplicate short name, long name or position.