Skip to content

Instantly share code, notes, and snippets.

@Rapptz
Created July 19, 2014 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rapptz/2d3703ae7b6414761b50 to your computer and use it in GitHub Desktop.
Save Rapptz/2d3703ae7b6414761b50 to your computer and use it in GitHub Desktop.
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.
#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