dlugolecki.net.pl
Dziennik
Polecane
Software
projects
/
command.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Extended example in README
[command.git]
/
README.md
diff --git
a/README.md
b/README.md
index cb2c84fe1fa129c03a1e2e1ec894623a3658bf39..b5cb7d54902934832af2c6726bce341141d70a42 100644
(file)
--- a/
README.md
+++ b/
README.md
@@
-6,8
+6,8
@@
C++ library for handling command line arguments.
### debian package
### debian package
- $ wget https://github.com/quayle/command/releases/download/v0.
2.1-deb/command_0.2.1
_all.deb
- $ sudo dpkg -i command_0.
2.1
_all.deb
+ $ 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
### from sources
@@
-30,21
+30,42
@@
As this is header-only library, you don't need any additional steps.
example.cpp:
#include <iostream>
example.cpp:
#include <iostream>
+ #include <string>
#include <command/command.h>
#include <command/option.h>
#include <command/command.h>
#include <command/option.h>
+ #include <command/argument.h>
using namespace command;
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[]) {
int main(int argc, char *argv[]) {
+ MyClass myClass;
try {
Command command(argc, argv, {
try {
Command command(argc, argv, {
- new Option<void>("-h", "Help", [](void) { std::cout << "Help information\n"; })
+ new Option<void>("-h", "Help", [](void) {
+ std::cout << "Help information\n";
+ }),
+ new Argument<std::string>("Value for MyClass",
+ std::bind(&MyClass::setValue, &myClass, std::placeholders::_1)
+ )
});
}
catch(const std::exception & e) {
return 1;
}
});
}
catch(const std::exception & e) {
return 1;
}
+ std::cout << myClass.getValue() << std::endl;
+
return 0;
}
return 0;
}
@@
-53,6
+74,14
@@
Now program can be compiled & run using following commands:
$ g++ -std=c++11 example.cpp
$ ./a.out -h
Help information
$ 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:
### Possible classes to use:
@@
-91,6
+120,7
@@
separated by given separator. For each value passed function is invoked:
)
More complex example:
)
More complex example:
+
(...)
void argument_function(bool a) {
std::cout << "Argument: " << a << std::endl;
(...)
void argument_function(bool a) {
std::cout << "Argument: " << a << std::endl;
@@
-119,7
+149,9
@@
More complex example:
Above code allows us to:
Above code allows us to:
-Parameters wrapped in Required class, have validator which checks if argument
+Parameters wrapped in Required class, have validator which checks if argument is
+passed:
+
$ ./a.out
*Input values* is required
$ ./a.out
*Input values* is required
@@
-136,6
+168,7
@@
Parameters wrapped in Required class, have validator which checks if argument
Option: f failed value conversion to the required type
For MultiValue Parameters each value is passed to the given function:
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
$ ./a.out 1-0-1
Argument: 1
Argument: 0