-
-
Save Rapptz/2d3703ae7b6414761b50 to your computer and use it in GitHub Desktop.
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
danny@debian:~/Documents/GitHub/Gears$ ./dev --help | |
usage: dev [options...] | |
program to help test command line parsing for fun | |
subcommands | |
add adds stuff | |
options: | |
-h, --help shows this message and exits | |
-v, --version shows version info and exits | |
--stuff set stuff | |
danny@debian:~/Documents/GitHub/Gears$ ./dev --stuff=10 | |
stuff: 10 | |
danny@debian:~/Documents/GitHub/Gears$ ./dev add -h | |
usage: dev add [options...] | |
options: | |
-h, --help shows this message and exits | |
-o, --other other stuff | |
danny@debian:~/Documents/GitHub/Gears$ ./dev add -o 15 | |
other: 15 | |
danny@debian:~/Documents/GitHub/Gears$ ./dev | |
usage: dev [options...] | |
dev: error: you messed up bub. |
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
#include <gears/optparse/option_parser.hpp> | |
using namespace gears; | |
int main(int argc, char** argv) { | |
optparse::option_parser parser; | |
parser.program_name = "dev"; | |
parser.description = "program to help test command line parsing for fun"; | |
// top level parser | |
parser.add<bool>("version", 'v', "shows version info and exits"); | |
parser.add<int>("stuff", '\0', "set stuff"); | |
parser.new_subcommand("add", "adds stuff") | |
.add<int>("other", 'o', "other stuff"); | |
auto&& args = parser.parse(argc, argv); | |
auto&& sub = parser.active_subcommand(); | |
if(sub.name == "add") { | |
std::cout << "other: " << sub.get_or<int>("other", 20) << '\n'; | |
} | |
else if(parser.is_active("stuff")) { | |
std::cout << "stuff: " << parser.get<int>("stuff") << '\n'; | |
} | |
else { | |
parser.error("you messed up bub."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment