Lightweight Command Line Input Parser for modern C++
An extremely simple to use, yet fully fledged, C++ Command Line Parser library (compiled and single-header-only).
Copy the single header file clip.hpp within your project and just use it.
Requires a C++17 compiler.
Building the library is the preferrable approach since it leads to faster compile and link times.
Requires CMake (> 3.8) and a C++17 compiler.
$ git clone https://github.com/stefanolusardi/clip.git
$ cd clip && mkdir build && cd build
$ cmake --build . --config Release
$ cmake --build . --config Debug
$ cmake --install . --prefix <ANYWHERE_YOU_LIKE>
// Create clip::CommandLineParser object
clip::CommandLineParser clp;
// Create and add clip::PositionalArgument
clip::PositionalArgument posArg("positional_arg");
clp.addPositionalArgument(posArg);
// Create and add clip::OptionalArgument
clip::OptionalArgument optArg({ "a", "a-opt", "a-opt-arg" }, "Opt Arg Description");
clp.addOptionalArgument(optArg);
// Create and add clip::OptionalArgument with:
// expected type (e.g. std::string), default value (e.g. "initial_value") and other options (e.g. isRequired)
clip::OptionalArgument optArgValue({ "c" }, "Description", clip::value<std::string>("initial_value").isRequired(true));
clp.addOptionalArgument(optArgValue);
// Invoke parse method
clp.parse(argc, argv);
if (clp.isSet(posArg))
{
std::cout << "positional_arg is set." << std::endl;
}
if (clp.isSet(optArgValue))
{
auto v = clp.getOptionValue(optArgValue);
std::cout << "Value: " << v << std::endl;
}
// -- clip::ArgumentValue<T> constructor is made private by design.
//clip::ArgumentValue<std::string> argValue = clip::ArgumentValue<std::string>(); // This line will not compile.
//clip::ArgumentValue<std::string>* argValue = new clip::ArgumentValue<std::string>(); // This line will not compile.
// -- It is possible to create a clip::ArgumentValue<T> using the clip::value<T>() function.
clip::ArgumentValue<std::string> argValue = clip::value<std::string>();
// -- C++17 automatic template deduction in declaration.
clip::ArgumentValue argValue = clip::value<int>();
// -- Default argument value can be provided using clip::value<T>(t) override.
clip::ArgumentValue argValue = clip::value<double>(3.14);
// -- Fluent API to specify further options (set required options and override default value).
clip::ArgumentValue argValue = clip::value<std::string>("initial_value").isRequired(true).value("some_value");
// -- Specify a list of option names to be used during parsing to recognize this option
clip::OptionalArgument optArg({ "a" });
clip::OptionalArgument optArg({ "a", "option", "another-long-option", "too-much-options" });
// -- Add an option description
clip::OptionalArgument optArg({ "a", "option" }, "This is a brief option description");
// -- Give the option a clip::ArgumentValue
// -- Note that since clip::OptionalArgument<T> is a template class, when a clip::ArgumentValue is not given, the default T used is std::nullptr_t
clip::OptionalArgument<std::string> optArg({ "a", "option" }, "Option Description", clip::value<std::string>("hello"));
// -- C++17 automatic template deduction in declaration.
clip::OptionalArgument optArg({ "a", "option" }, "Option Description", clip::value<std::string>("hello"));
// -- Exploit clip::ArgumentValue fluent API to specify argument value directly inline.
clip::OptionalArgument optArg({ "a", "option" }, "Option Description", clip::value<std::string>("hello").isRequired(true).value("some value"););
Examples are located within the examples folder.
It is possible to build all the examples using:
$ git clone https://github.com/stefanolusardi/clip.git
$ cd clip && mkdir build && cd build
$ cmake --build . --config Release -DBUILD_EXAMPLES=ON
Coming soon