3 C++ library for handling command line arguments.
5 [![Build Status](https://travis-ci.org/quayle/command.svg?branch=master)](https://travis-ci.org/quayle/command)
11 $ wget https://github.com/quayle/command/releases/download/v0.3-deb/command_0.3_all.deb
12 $ sudo dpkg -i command_0.3_all.deb
16 You will need to have autotools installed (automake, autoconf, ...)
25 You need to enable c++11 support in your compiler. You can achieve that in
26 g++ and clang++ by adding `-std=c++11` compilation flag.
28 As this is header-only library, you don't need any additional steps.
36 #include <command/command.h>
37 #include <command/option.h>
38 #include <command/argument.h>
40 using namespace command;
43 std::string _value = "Default";
45 void setValue(std::string value) {
48 std::string getValue() {
49 return std::string("Value from MyClass: ") + this->_value;
53 int main(int argc, char *argv[]) {
56 Command command(argc, argv, {
57 new Option<void>("-h", "Help", [](void) {
58 std::cout << "Help information\n";
60 new Argument<std::string>("Value for MyClass",
61 std::bind(&MyClass::setValue, &myClass, std::placeholders::_1)
65 catch(const std::exception & e) {
69 std::cout << myClass.getValue() << std::endl;
74 Now program can be compiled & run using following commands:
76 $ g++ -std=c++11 example.cpp
79 Value from MyClass: Default
82 Value from MyClass: someArg
86 Value from MyClass: someArg
88 ### Possible classes to use:
90 Arguments are non-named program parameters. They must have some description and
91 function handling when argument is passed:
93 new Argument<bool>("Bool argument", [](bool value) { });
95 Options are named program parameters. Option need name (e.g.: "i"), description,
98 new Option<int>("name", "Integer option", [](int value) { });
100 Options could also be set as containing no value. In that case they become just
101 a simple switches (some kind of mix between Argument and Option). They are used
102 just to invoke some function if specific name was passed:
104 new Option<void>("v", "Verbose mode of program", [](void) { });
108 Parameters (Options and Arguments) can also be wrapped in Behaviours.
110 Required behaviour - if specific parameter was not passed and is required,
111 exception is thrown (missingRequiredParameter):
114 new Argument<bool>("Bool argument", [](bool value) { })
117 MultiValue behaviour - given parameter can handle more than one value. Values are
118 separated by given separator. For each value passed function is invoked:
121 new Option<std::string>("input", "Input file", [](std::string value) { })
124 More complex example:
127 void argument_function(bool a) {
128 std::cout << "Argument: " << a << std::endl;
131 void option_function(std::string a) {
132 std::cout << "Option function " << a << std::endl;
135 void void_function(void) {
136 std::cout << "Void function " << std::endl;
139 Command command(argc, argv, {
142 new Argument<bool>("Input values", argument_function)
146 new Option<std::string>("f", "Optional file", option_function)
148 new Option<void>("h", "Help", void_function)
152 Above code allows us to:
154 Parameters wrapped in Required class, have validator which checks if argument is
158 *Input values* is required
167 Option: f requires value to be specified after equal sign, but no equal sign was found
170 Option: f failed value conversion to the required type
172 For MultiValue Parameters each value is passed to the given function:
179 $ ./command 0 f=one,two,three
183 Option function three
187 Current documentation can be found at:
188 http://dlugolecki.net.pl/software/command/docs/
190 If for some reason it is unavailable, you can build it yourself. The only
191 requirement is to have [Doxygen](http://www.doxygen.org/) installed when `make` command is invoked.