3 C++ library for handling command line arguments.
9 $ wget https://github.com/quayle/command/releases/download/v0.3-deb/command_0.3_all.deb
10 $ sudo dpkg -i command_0.3_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.
34 #include <command/command.h>
35 #include <command/option.h>
36 #include <command/argument.h>
38 using namespace command;
41 std::string _value = "Default";
43 void setValue(std::string value) {
46 std::string getValue() {
47 return std::string("Value from MyClass: ") + this->_value;
51 int main(int argc, char *argv[]) {
54 Command command(argc, argv, {
55 new Option<void>("-h", "Help", [](void) {
56 std::cout << "Help information\n";
58 new Argument<std::string>("Value for MyClass",
59 std::bind(&MyClass::setValue, &myClass, std::placeholders::_1)
63 catch(const std::exception & e) {
67 std::cout << myClass.getValue() << std::endl;
72 Now program can be compiled & run using following commands:
74 $ g++ -std=c++11 example.cpp
77 Value from MyClass: Default
80 Value from MyClass: someArg
84 Value from MyClass: someArg
86 ### Possible classes to use:
88 Arguments are non-named program parameters. They must have some description and
89 function handling when argument is passed:
91 new Argument<bool>("Bool argument", [](bool value) { });
93 Options are named program parameters. Option need name (e.g.: "i"), description,
96 new Option<int>("name", "Integer option", [](int value) { });
98 Options could also be set as containing no value. In that case they become just
99 a simple switches (some kind of mix between Argument and Option). They are used
100 just to invoke some function if specific name was passed:
102 new Option<void>("v", "Verbose mode of program", [](void) { });
106 Parameters (Options and Arguments) can also be wrapped in Behaviours.
108 Required behaviour - if specific parameter was not passed and is required,
109 exception is thrown (missingRequiredParameter):
112 new Argument<bool>("Bool argument", [](bool value) { })
115 MultiValue behaviour - given parameter can handle more than one value. Values are
116 separated by given separator. For each value passed function is invoked:
119 new Option<std::string>("input", "Input file", [](std::string value) { })
122 More complex example:
125 void argument_function(bool a) {
126 std::cout << "Argument: " << a << std::endl;
129 void option_function(std::string a) {
130 std::cout << "Option function " << a << std::endl;
133 void void_function(void) {
134 std::cout << "Void function " << std::endl;
137 Command command(argc, argv, {
140 new Argument<bool>("Input values", argument_function)
144 new Option<std::string>("f", "Optional file", option_function)
146 new Option<void>("h", "Help", void_function)
150 Above code allows us to:
152 Parameters wrapped in Required class, have validator which checks if argument is
156 *Input values* is required
165 Option: f requires value to be specified after equal sign, but no equal sign was found
168 Option: f failed value conversion to the required type
170 For MultiValue Parameters each value is passed to the given function:
177 $ ./command 0 f=one,two,three
181 Option function three
185 Current documentation can be found at:
186 http://dlugolecki.net.pl/software/command/docs/
188 If for some reason it is unavailable, you can build it yourself. The only
189 requirement is to have [Doxygen](http://www.doxygen.org/) installed when `make` command is invoked.