Header-only C++ library for parsing program parameters from command-line arguments.
#include <program_params/program_params.h>
int main (int argc, char *argv[])
{
size_t count = 10;
float interval = 1.0f;
std::string destination;
program_params::Params params;
params.add(count, {"-c", "--count"});
params.add(interval, {"-i", "--interval"});
params.add(destination, {}, true);
try
{
params.parse(argc - 1, argv + 1);
}
catch (const params::Exception &ex)
{
// Print usage and exit.
}
// Do something useful with your params.
}
Using external storage (type is inferred):
params.add(storage, names, required)
Using internal storage (type must be specified):
params.add<T>(names, required)
Positional parameters may have no name:
params.add(storage, {}, required)
params.get<T>(name)
Parameters with external storage are stored directly to the provided storage.
-
Short and long options, allowing multiple names:
params.add(count, {"-c"}); params.add(interval, {"--interval"}); params.add(size, {"-s", "--size"});
-
Positional parameters (non-options) with optional name:
params.add(destination, {}); params.add(destination, {"destination"}); params.add<std::string>({"destination"});
Name needed to retrieve the value with internal storage.
-
Required options or positional parameters:
params.add(count, {"-c"}, true); params.add(destination, {}, true);
-
Inferring type for external storage:
bool flag; int count; params.add(flag, {"-f"}); params.add(count, {"-c"});
-
Internal storage with explicit type:
params.add<size_t>({"-c", "--count"}); params.add<std::string>({"destination"}); // Parse params. params.get<size_t>({"-c"}); params.get<size_t>({"--count"}); params.get<std::string>({"destination"});
-
Strict argument checking (doesn't allow unknown arguments):
program_params::Params(true);
-
Unknown arguments allowed by default:
program_params::Params(false); program_params::Params();
-
Short options without values (flags) in single argument:
params.add<bool>({"-a"}); params.add<bool>({"-s"}); params.add<bool>({"-d"}); params.add<bool>({"-f"});
program -asdf
-
Short option with value in single argument:
program -i2.5
-
Long option with value in single argument:
program --interval=2.5
-
Long option with value as separate arguments:
program --interval 2.5