Skip to content

Instantly share code, notes, and snippets.

@Rapptz

Rapptz/dev.cpp Secret

Last active August 29, 2015 14:04
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/2da8f0a4e2a102caa256 to your computer and use it in GitHub Desktop.
Save Rapptz/2da8f0a4e2a102caa256 to your computer and use it in GitHub Desktop.
#include <gears/optparse.hpp>
#include <iostream>
namespace opt = gears::optparse;
int main(int argc, char** argv) {
int stuff = 0;
opt::option_parser parser = {{
{"stuff", "modifies stuff", opt::bind_to(stuff) },
{'f', "returns 42", opt::constant(42) },
{"test", 't', "requires a value", opt::value<int>() },
{"list", 'l', "requires a list", opt::list<std::vector<int>>(3) },
{"compose", 'c', "composes", opt::compose<std::vector<int>>() }
}};
parser.program_name = "dev";
parser.description = "A test program to test the different ways of handling command line arguments.";
auto&& args = parser.parse(argv, argv + argc);
if(args.options.is_active("list")) {
std::cout << "list:\n";
for(auto&& i : args.options.get<std::vector<int>>("list")) {
std::cout << i << '\n';
}
}
if(args.options.is_active('f')) {
std::cout << "f has returned: " << args.options.get<int>('f') << '\n';
}
if(args.options.is_active("test")) {
std::cout << "test has returned: " << args.options.get<int>("test") << '\n';
}
if(args.options.is_active("compose")) {
std::cout << "compose:\n";
for(auto&& i : args.options.get<std::vector<int>>("compose")) {
std::cout << i << '\n';
}
}
if(!args.positional.empty()) {
std::cout << "positional: \n";
for(auto&& i : args.positional) {
std::cout << i << '\n';
}
}
std::cout << "stuff: " << stuff << '\n';
}
danny@debian:~/Documents/GitHub/Gears$ ./dev
stuff: 0
danny@debian:~/Documents/GitHub/Gears$ ./dev --stuff=42
stuff: 42
danny@debian:~/Documents/GitHub/Gears$ ./dev -f --stuff=20
f has returned: 42
stuff: 20
danny@debian:~/Documents/GitHub/Gears$ ./dev -c 10 --stuff=40 -c 20
compose:
10
20
stuff: 40
danny@debian:~/Documents/GitHub/Gears$ ./dev -t 10 --list 1 2 3 -c 10 --stuff=20 -c 30 -- 1 2 3
list:
1
2
3
test has returned: 10
compose:
10
30
positional:
1
2
3
stuff: 20
danny@debian:~/Documents/GitHub/Gears$ ./dev --help
usage: dev [options...]
A test program to test the different ways of handling command line arguments.
options:
-h, --help shows this message and exits
--stuff modifies stuff
-f returns 42
-t, --test requires a value
-l, --list requires a list
-c, --compose composes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment