Fixes Issue #23

This commit is contained in:
Pranav Srinivas Kumar 2019-06-05 15:30:08 -04:00 committed by GitHub
parent af650392e7
commit 3ff61cce4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,7 +45,14 @@ int main(int argc, char *argv[]) {
.help("display the square of a given integer") .help("display the square of a given integer")
.action([](const std::string& value) { return std::stoi(value); }); .action([](const std::string& value) { return std::stoi(value); });
program.parse_args(argc, argv); try {
program.parse_args({ "./main", "--help" });
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
auto input = program.get<int>("square"); auto input = program.get<int>("square");
std::cout << (input * input) << std::endl; std::cout << (input * input) << std::endl;
@ -80,7 +87,14 @@ program.add_argument("--verbose")
.default_value(false) .default_value(false)
.implicit_value(true); .implicit_value(true);
program.parse_args(argc, argv); try {
program.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
if (program["--verbose"] == true) { if (program["--verbose"] == true) {
std::cout << "Verbosity enabled" << std::endl; std::cout << "Verbosity enabled" << std::endl;
@ -110,7 +124,14 @@ program.add_argument("--verbose")
.default_value(false) .default_value(false)
.implicit_value(true); .implicit_value(true);
program.parse_args(argc, argv); try {
program.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
int input = program.get<int>("square"); int input = program.get<int>("square");
@ -160,7 +181,14 @@ program.add_argument("--input_files")
.help("The list of input files") .help("The list of input files")
.nargs(2); .nargs(2);
program.parse_args(argc, argv); // Example: ./main --input_files config.yml System.xml try {
program.parse_args(argc, argv); // Example: ./main --input_files config.yml System.xml
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
auto files = program.get<std::vector<std::string>>("--input_files"); // {"config.yml", "System.xml"} auto files = program.get<std::vector<std::string>>("--input_files"); // {"config.yml", "System.xml"}
``` ```
@ -182,7 +210,14 @@ program.add_argument("--query_point")
.default_value(std::vector<double>{0.0, 0.0, 0.0}) .default_value(std::vector<double>{0.0, 0.0, 0.0})
.action([](const std::string& value) { return std::stod(value); }); .action([](const std::string& value) { return std::stod(value); });
program.parse_args(argc, argv); // Example: ./main --query_point 3.5 4.7 9.2 try {
program.parse_args(argc, argv); // Example: ./main --query_point 3.5 4.7 9.2
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
auto query_point = program.get<std::vector<double>>("--query_point"); // {3.5, 4.7, 9.2} auto query_point = program.get<std::vector<double>>("--query_point"); // {3.5, 4.7, 9.2}
``` ```
@ -207,7 +242,14 @@ program.add_argument("-c")
.default_value(std::vector<float>{0.0f, 0.0f}) .default_value(std::vector<float>{0.0f, 0.0f})
.action([](const std::string& value) { return std::stof(value); }); .action([](const std::string& value) { return std::stof(value); });
program.parse_args(argc, argv); // Example: ./main -abc 1.95 2.47 try {
program.parse_args(argc, argv); // Example: ./main -abc 1.95 2.47
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
auto a = program.get<bool>("-a"); // true auto a = program.get<bool>("-a"); // true
auto b = program.get<bool>("-b"); // true auto b = program.get<bool>("-b"); // true
@ -276,7 +318,14 @@ program.add_argument("config")
return config_json; return config_json;
}); });
program.parse_args({"./test", "config.json"}); try {
program.parse_args({"./test", "config.json"});
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
nlohmann::json config = program.get<nlohmann::json>("config"); nlohmann::json config = program.get<nlohmann::json>("config");
``` ```
@ -305,7 +354,14 @@ program.add_argument("-c")
program.add_argument("--files") program.add_argument("--files")
.nargs(3); .nargs(3);
program.parse_args(argc, argv); try {
program.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
auto numbers = program.get<std::vector<int>>("numbers"); // {1, 2, 3} auto numbers = program.get<std::vector<int>>("numbers"); // {1, 2, 3}
auto a = program.get<bool>("-a"); // true auto a = program.get<bool>("-a"); // true
@ -340,7 +396,14 @@ program.add_argument("input")
return std::string{ "baz" }; return std::string{ "baz" };
}); });
program.parse_args(argc, argv); try {
program.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
program.print_help();
exit(0);
}
auto input = program.get("input"); auto input = program.get("input");
std::cout << input << std::endl; std::cout << input << std::endl;