Argolis is a no-nonsense, small, header-only, callback-based command line parsing library for C++, with minimal but effective error checking capabilities.
- Callback-based: you register your handlers for options, arguments and errors, and they will be invoked synchronously during the parsing phase, no need for a subsequent step asking "did you see option '-x' ?"
- Options allowed in short (
-a
) or, optionally, long (--all
,-all
) form - Options can be specified to require / forbid / do not mind being followed by an argument
- Option arguments may follow the option itself (
--count 42
) or be joined to it in a key-value form (--count=42
) - Combi options can be allowed (e.g.
-avc <arg>
being equivalent to-a -v -c <arg>
) - "End of options" marker (i.e.
--
) supported - Include-only library
- Doxygen documentation
- Requires C++17
C++17
Just copy include/argolis.hpp
to your project's include directory
Instantiate a Parser
object, with as many Opt_Spec
objects you want. In each Opt_Spec
object put:
- the "short" name of the option (e.g. 'v')
- optionally, the "long" name of the option (e.g. "verbose")
- choose what the argument policy will be for this option:
NO_ARG
this option will not accept an argumentMAYBE_ARG
this option might accept an argumentEXPECT_ARG
this option requires an argument
- the Callable action you want to be called when this option is met on the command line
using namespace argolis;
Parser parser({
{'a',"all",Opt_Spec::Arg_Policy::NO_ARG,[](const Opt_Spec &,std::optional<std::string_view>){ /* YOUR CODE HANDLING OPTION "-a" */}}
/* ... MORE OPTIONS SPECIFICATIONS */
});
Assign a Callable action you want to be called for each free-standing argument
parser.on_arg([](std::string_view){ /* YOUR CODE */ });
Assign a Callable action you want to be called when a parsing error occurs
parser.on_err([](std::string_view){ /* YOUR CODE */ });
Start parsing
parser.parse(argc,argv);
make doc
make example
(see examples directory/page for more)
#include "argolis.hpp"
int main(int argc, char * argv[]) {
using namespace argolis;
Parser p({
{'a',"all",Opt_Spec::Arg_Policy::NO_ARG,[](auto,auto){ /* WE GOT OPTION "all" ! */}},
{'v',"verbose",Opt_Spec::Arg_Policy::MAYBE_ARG,[](auto,auto){ /* WE GOT OPTION "verbose" ! */}},
{'z',Opt_Spec::Arg_Policy::MAYBE_ARG,[](auto,auto){ /* WE GOT OPTION 'z' ! */}},
{'n',"num",Opt_Spec::Arg_Policy::EXPECT_ARG,[](auto,auto){ /* WE GOT OPTION "num" ! */}}
});
p.combi_allowed(true);
p.abort_on_error(true);
p.on_err([](Error e){ /* WE GOT AN ERR! */ });
p.on_err([](std::string_view a){ /* WE GOT A FREE-STANDING ARGUMENT! */ });
p.parse(argc,argv);
return 0;
}
Distributed under the MIT License. See LICENSE.txt for more information.