X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=README.md;h=b5cb7d54902934832af2c6726bce341141d70a42;hb=e0fda02032d7d502e57e24eb218da9e24155541a;hp=037aea026122332b3038f2b78e4b06c9931d26df;hpb=07fd07c0e8dbaa53cb7f1ebd3c01a1770815240f;p=command.git diff --git a/README.md b/README.md index 037aea0..b5cb7d5 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,18 @@ # Command -C++ library for handling command line arguments, designed to be easily used. +C++ library for handling command line arguments. ## Installation +### debian package + + $ wget https://github.com/quayle/command/releases/download/v0.3-deb/command_0.3_all.deb + $ sudo dpkg -i command_0.3_all.deb + +### from sources + +You will need to have autotools installed (automake, autoconf, ...) + $ ./autogen.sh $ ./configure $ make @@ -21,21 +30,42 @@ As this is header-only library, you don't need any additional steps. example.cpp: #include + #include #include #include + #include using namespace command; + class MyClass { + std::string _value = "Default"; + public: + void setValue(std::string value) { + this->_value = value; + } + std::string getValue() { + return std::string("Value from MyClass: ") + this->_value; + } + }; + int main(int argc, char *argv[]) { + MyClass myClass; try { Command command(argc, argv, { - new Option("-h", "Help", [](void) { std::cout << "Help information\n"; }) + new Option("-h", "Help", [](void) { + std::cout << "Help information\n"; + }), + new Argument("Value for MyClass", + std::bind(&MyClass::setValue, &myClass, std::placeholders::_1) + ) }); } catch(const std::exception & e) { return 1; } + std::cout << myClass.getValue() << std::endl; + return 0; } @@ -44,6 +74,111 @@ Now program can be compiled & run using following commands: $ g++ -std=c++11 example.cpp $ ./a.out -h Help information + Value from MyClass: Default + + $ ./a.out someArg + Value from MyClass: someArg + + $ ./a.out someArg -h + Help information + Value from MyClass: someArg + +### Possible classes to use: + +Arguments are non-named program parameters. They must have some description and +function handling when argument is passed: + + new Argument("Bool argument", [](bool value) { }); + +Options are named program parameters. Option need name (e.g.: "i"), description, +and function. + + new Option("name", "Integer option", [](int value) { }); + +Options could also be set as containing no value. In that case they become just +a simple switches (some kind of mix between Argument and Option). They are used +just to invoke some function if specific name was passed: + + new Option("v", "Verbose mode of program", [](void) { }); + +### Behaviours + +Parameters (Options and Arguments) can also be wrapped in Behaviours. + +Required behaviour - if specific parameter was not passed and is required, +exception is thrown (missingRequiredParameter): + + new Required( + new Argument("Bool argument", [](bool value) { }) + ); + +MultiValue behaviour - given parameter can handle more than one value. Values are +separated by given separator. For each value passed function is invoked: + + new MultiValue(",", + new Option("input", "Input file", [](std::string value) { }) + ) + +More complex example: + + (...) + void argument_function(bool a) { + std::cout << "Argument: " << a << std::endl; + } + + void option_function(std::string a) { + std::cout << "Option function " << a << std::endl; + } + + void void_function(void) { + std::cout << "Void function " << std::endl; + } + + Command command(argc, argv, { + new Required( + new MultiValue("-", + new Argument("Input values", argument_function) + ) + ), + new MultiValue(",", + new Option("f", "Optional file", option_function) + ) + new Option("h", "Help", void_function) + }); + (...) + +Above code allows us to: + +Parameters wrapped in Required class, have validator which checks if argument is +passed: + + $ ./a.out + *Input values* is required + + $ ./a.out 1 + Argument: 1 + + $ ./a.out 0 + Argument: 0 + + $ ./command 0 f + Option: f requires value to be specified after equal sign, but no equal sign was found + + $ ./command 0 f= + Option: f failed value conversion to the required type + +For MultiValue Parameters each value is passed to the given function: + + $ ./a.out 1-0-1 + Argument: 1 + Argument: 0 + Argument: 1 + + $ ./command 0 f=one,two,three + Argument: 0 + Option function one + Option function two + Option function three ## Documentation @@ -51,4 +186,4 @@ Current documentation can be found at: http://dlugolecki.net.pl/software/command/docs/ If for some reason it is unavailable, you can build it yourself. The only -requirement is to have [Doxygen](www.doxygen.org/) installed when `make` command is invoked. +requirement is to have [Doxygen](http://www.doxygen.org/) installed when `make` command is invoked.