3 C++ library for handling command line arguments.
9 $ wget https://github.com/quayle/command/releases/download/v0.2.1-deb/command_0.2.1_all.deb
10 $ sudo dpkg -i command_0.2.1_all.deb
14 You will need to have autotools installed (automake, autoconf, ...)
23 You need to enable c++11 support in your compiler. You can achieve that in
24 g++ and clang++ by adding `-std=c++11` compilation flag.
26 As this is header-only library, you don't need any additional steps.
33 #include <command/command.h>
34 #include <command/option.h>
36 using namespace command;
38 int main(int argc, char *argv[]) {
40 Command command(argc, argv, {
41 new Option<void>("-h", "Help", [](void) { std::cout << "Help information\n"; })
44 catch(const std::exception & e) {
51 Now program can be compiled & run using following commands:
53 $ g++ -std=c++11 example.cpp
57 ### Possible classes to use:
59 Arguments are non-named program parameters. They must have some description and
60 function handling when argument is passed:
62 new Argument<bool>("Bool argument", [](bool value) { });
64 Options are named program parameters. Option need name (e.g.: "i"), description,
67 new Option<int>("name", "Integer option", [](int value) { });
69 Options could also be set as containing no value. In that case they become just
70 a simple switches (some kind of mix between Argument and Option). They are used
71 just to invoke some function if specific name was passed:
73 new Option<void>("v", "Verbose mode of program", [](void) { });
77 Parameters (Options and Arguments) can also be wrapped in Behaviours.
79 Required behaviour - if specific parameter was not passed and is required,
80 exception is thrown (missingRequiredParameter):
83 new Argument<bool>("Bool argument", [](bool value) { })
86 MultiValue behaviour - given parameter can handle more than one value. Values are
87 separated by given separator. For each value passed function is invoked:
90 new Option<std::string>("input", "Input file", [](std::string value) { })
96 void argument_function(bool a) {
97 std::cout << "Argument: " << a << std::endl;
100 void option_function(std::string a) {
101 std::cout << "Option function " << a << std::endl;
104 void void_function(void) {
105 std::cout << "Void function " << std::endl;
108 Command command(argc, argv, {
111 new Argument<bool>("Input values", argument_function)
115 new Option<std::string>("f", "Optional file", option_function)
117 new Option<void>("h", "Help", void_function)
121 Above code allows us to:
123 Parameters wrapped in Required class, have validator which checks if argument is
127 *Input values* is required
136 Option: f requires value to be specified after equal sign, but no equal sign was found
139 Option: f failed value conversion to the required type
141 For MultiValue Parameters each value is passed to the given function:
148 $ ./command 0 f=one,two,three
152 Option function three
156 Current documentation can be found at:
157 http://dlugolecki.net.pl/software/command/docs/
159 If for some reason it is unavailable, you can build it yourself. The only
160 requirement is to have [Doxygen](http://www.doxygen.org/) installed when `make` command is invoked.