forked from graspit-simulator/graspit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switched out cpp_argparse for cmdline arg parse
- Loading branch information
Showing
14 changed files
with
1,243 additions
and
1,159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Copyright (c) 2009, Hideyuki Tanaka | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
|
||
* Neither the name of Hideyuki Tanaka nor the names of other | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
============================================= | ||
cmdline: A simple command line parser for C++ | ||
============================================= | ||
|
||
About | ||
----- | ||
|
||
This is a simple command line parser for C++. | ||
|
||
- Easy to use | ||
- Only one header file | ||
- Automatic type check | ||
|
||
Sample | ||
------ | ||
|
||
Here show sample usages of cmdline. | ||
|
||
Normal usage | ||
============ | ||
|
||
This is an example of simple usage. | ||
|
||
:: | ||
|
||
// include cmdline.h | ||
#include "cmdline.h" | ||
int main(int argc, char *argv[]) | ||
{ | ||
// create a parser | ||
cmdline::parser a; | ||
// add specified type of variable. | ||
// 1st argument is long name | ||
// 2nd argument is short name (no short name if '\0' specified) | ||
// 3rd argument is description | ||
// 4th argument is mandatory (optional. default is false) | ||
// 5th argument is default value (optional. it used when mandatory is false) | ||
a.add<string>("host", 'h', "host name", true, ""); | ||
// 6th argument is extra constraint. | ||
// Here, port number must be 1 to 65535 by cmdline::range(). | ||
a.add<int>("port", 'p', "port number", false, 80, cmdline::range(1, 65535)); | ||
// cmdline::oneof() can restrict options. | ||
a.add<string>("type", 't', "protocol type", false, "http", cmdline::oneof<string>("http", "https", "ssh", "ftp")); | ||
// Boolean flags also can be defined. | ||
// Call add method without a type parameter. | ||
a.add("gzip", '\0', "gzip when transfer"); | ||
// Run parser. | ||
// It returns only if command line arguments are valid. | ||
// If arguments are invalid, a parser output error msgs then exit program. | ||
// If help flag ('--help' or '-?') is specified, a parser output usage message then exit program. | ||
a.parse_check(argc, argv); | ||
// use flag values | ||
cout << a.get<string>("type") << "://" | ||
<< a.get<string>("host") << ":" | ||
<< a.get<int>("port") << endl; | ||
// boolean flags are referred by calling exist() method. | ||
if (a.exist("gzip")) cout << "gzip" << endl; | ||
} | ||
|
||
Here are some execution results: | ||
|
||
:: | ||
|
||
$ ./test | ||
usage: ./test --host=string [options] ... | ||
options: | ||
-h, --host host name (string) | ||
-p, --port port number (int [=80]) | ||
-t, --type protocol type (string [=http]) | ||
--gzip gzip when transfer | ||
-?, --help print this message | ||
|
||
:: | ||
|
||
$ ./test -? | ||
usage: ./test --host=string [options] ... | ||
options: | ||
-h, --host host name (string) | ||
-p, --port port number (int [=80]) | ||
-t, --type protocol type (string [=http]) | ||
--gzip gzip when transfer | ||
-?, --help print this message | ||
|
||
:: | ||
|
||
$ ./test --host=github.com | ||
http://github.com:80 | ||
|
||
:: | ||
|
||
$ ./test --host=github.com -t ftp | ||
ftp://github.com:80 | ||
|
||
:: | ||
|
||
$ ./test --host=github.com -t ttp | ||
option value is invalid: --type=ttp | ||
usage: ./test --host=string [options] ... | ||
options: | ||
-h, --host host name (string) | ||
-p, --port port number (int [=80]) | ||
-t, --type protocol type (string [=http]) | ||
--gzip gzip when transfer | ||
-?, --help print this message | ||
|
||
:: | ||
|
||
$ ./test --host=github.com -p 4545 | ||
http://github.com:4545 | ||
|
||
:: | ||
|
||
$ ./test --host=github.com -p 100000 | ||
option value is invalid: --port=100000 | ||
usage: ./test --host=string [options] ... | ||
options: | ||
-h, --host host name (string) | ||
-p, --port port number (int [=80]) | ||
-t, --type protocol type (string [=http]) | ||
--gzip gzip when transfer | ||
-?, --help print this message | ||
|
||
:: | ||
|
||
$ ./test --host=github.com --gzip | ||
http://github.com:80 | ||
gzip | ||
|
||
Extra Options | ||
============= | ||
|
||
- rest of arguments | ||
|
||
Rest of arguments are referenced by rest() method. | ||
It returns vector of string. | ||
Usualy, they are used to specify filenames, and so on. | ||
|
||
:: | ||
|
||
for (int i = 0; i < a.rest().size(); i++) | ||
cout << a.rest()[i] << endl\; | ||
|
||
- footer | ||
|
||
footer() method is add a footer text of usage. | ||
|
||
:: | ||
|
||
... | ||
a.footer("filename ..."); | ||
... | ||
|
||
Result is: | ||
|
||
:: | ||
|
||
$ ./test | ||
usage: ./test --host=string [options] ... filename ... | ||
options: | ||
-h, --host host name (string) | ||
-p, --port port number (int [=80]) | ||
-t, --type protocol type (string [=http]) | ||
--gzip gzip when transfer | ||
-?, --help print this message | ||
|
||
- program name | ||
|
||
A parser shows program name to usage message. | ||
Default program name is determin by argv[0]. | ||
set_program_name() method can set any string to program name. | ||
|
||
Process flags manually | ||
---------------------- | ||
|
||
parse_check() method parses command line arguments and | ||
check error and help flag. | ||
|
||
You can do this process mannually. | ||
bool parse() method parses command line arguments then | ||
returns if they are valid. | ||
You should check the result, and do what you want yourself. | ||
|
||
(For more information, you may read test2.cpp.) |
Oops, something went wrong.