From 1a7a1dfd43970e690c849e8b9cb073308b6a943c Mon Sep 17 00:00:00 2001 From: cobyj33 Date: Wed, 25 Oct 2023 20:33:24 -0500 Subject: [PATCH] Change parse_args examples to catch const std::exception instead of std::runtime_error parse_args can throw exceptions that are not based on std::runtime_error, and the error message should show on all errors. --- README.md | 36 +++++++++---------- samples/compound_arguments.cpp | 2 +- samples/custom_assignment_characters.cpp | 2 +- samples/custom_prefix_characters.cpp | 2 +- samples/gathering_remaining_arguments.cpp | 2 +- samples/is_used.cpp | 2 +- .../joining_repeated_optional_arguments.cpp | 2 +- samples/list_of_arguments.cpp | 2 +- samples/negative_numbers.cpp | 2 +- samples/optional_flag_argument.cpp | 2 +- samples/positional_argument.cpp | 2 +- samples/required_optional_argument.cpp | 2 +- samples/subcommands.cpp | 2 +- 13 files changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 9cb0293d..6bf20462 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); } - catch (const std::runtime_error& err) { + catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; @@ -137,7 +137,7 @@ program.add_argument("--verbose") try { program.parse_args(argc, argv); } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -203,7 +203,7 @@ program.add_argument("--color") try { program.parse_args(argc, argv); // Example: ./main --color orange } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -226,7 +226,7 @@ program.add_argument("--color") try { program.parse_args(argc, argv); // Example: ./main --color red --color green --color blue } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -274,7 +274,7 @@ program.add_argument("floats") try { program.parse_args(argc, argv); } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -307,7 +307,7 @@ program.add_argument("--verbose") try { program.parse_args(argc, argv); } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -407,7 +407,7 @@ program.add_argument("--input_files") try { program.parse_args(argc, argv); // Example: ./main --input_files config.yml System.xml } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -436,7 +436,7 @@ program.add_argument("--query_point") try { program.parse_args(argc, argv); // Example: ./main --query_point 3.5 4.7 9.2 } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -491,7 +491,7 @@ program.add_argument("-c") try { program.parse_args(argc, argv); // Example: ./main -abc 1.95 2.47 } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -604,7 +604,7 @@ program.add_argument("files") try { program.parse_args(argc, argv); } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -651,7 +651,7 @@ program.add_argument("files") try { program.parse_args(argc, argv); } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -777,7 +777,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); } - catch (const std::runtime_error& err) { + catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; @@ -904,7 +904,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); } - catch (const std::runtime_error& err) { + catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; @@ -952,7 +952,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); } - catch (const std::runtime_error& err) { + catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; @@ -993,7 +993,7 @@ program.add_argument("config") try { program.parse_args({"./test", "config.json"}); } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -1029,7 +1029,7 @@ program.add_argument("--files") try { program.parse_args(argc, argv); } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -1071,7 +1071,7 @@ program.add_argument("input") try { program.parse_args(argc, argv); } -catch (const std::runtime_error& err) { +catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; std::exit(1); @@ -1100,7 +1100,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); } - catch (const std::runtime_error& err) { + catch (const std::exception& err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/compound_arguments.cpp b/samples/compound_arguments.cpp index 4cfc037f..c2f6d531 100644 --- a/samples/compound_arguments.cpp +++ b/samples/compound_arguments.cpp @@ -16,7 +16,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); // Example: ./main -abc 1.95 2.47 - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/custom_assignment_characters.cpp b/samples/custom_assignment_characters.cpp index 7e35ae0b..d9e4ca38 100644 --- a/samples/custom_assignment_characters.cpp +++ b/samples/custom_assignment_characters.cpp @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/custom_prefix_characters.cpp b/samples/custom_prefix_characters.cpp index 9f8917a3..058ba9f8 100644 --- a/samples/custom_prefix_characters.cpp +++ b/samples/custom_prefix_characters.cpp @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/gathering_remaining_arguments.cpp b/samples/gathering_remaining_arguments.cpp index be13f10f..099b07f8 100644 --- a/samples/gathering_remaining_arguments.cpp +++ b/samples/gathering_remaining_arguments.cpp @@ -9,7 +9,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/is_used.cpp b/samples/is_used.cpp index ffc05f88..91134eef 100644 --- a/samples/is_used.cpp +++ b/samples/is_used.cpp @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); // Example: ./main --color orange - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/joining_repeated_optional_arguments.cpp b/samples/joining_repeated_optional_arguments.cpp index 0f6ab815..99f1f6e9 100644 --- a/samples/joining_repeated_optional_arguments.cpp +++ b/samples/joining_repeated_optional_arguments.cpp @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { try { program.parse_args( argc, argv); // Example: ./main --color red --color green --color blue - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/list_of_arguments.cpp b/samples/list_of_arguments.cpp index 996e7480..b808725d 100644 --- a/samples/list_of_arguments.cpp +++ b/samples/list_of_arguments.cpp @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { try { program.parse_args( argc, argv); // Example: ./main --input_files config.yml System.xml - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/negative_numbers.cpp b/samples/negative_numbers.cpp index 319245fe..80fad6a6 100644 --- a/samples/negative_numbers.cpp +++ b/samples/negative_numbers.cpp @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/optional_flag_argument.cpp b/samples/optional_flag_argument.cpp index f935ecf0..b9bf4976 100644 --- a/samples/optional_flag_argument.cpp +++ b/samples/optional_flag_argument.cpp @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/positional_argument.cpp b/samples/positional_argument.cpp index 4343863c..07efc6ac 100644 --- a/samples/positional_argument.cpp +++ b/samples/positional_argument.cpp @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/required_optional_argument.cpp b/samples/required_optional_argument.cpp index 0271f500..4a935117 100644 --- a/samples/required_optional_argument.cpp +++ b/samples/required_optional_argument.cpp @@ -11,7 +11,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1; diff --git a/samples/subcommands.cpp b/samples/subcommands.cpp index ba1059df..97b3bff8 100644 --- a/samples/subcommands.cpp +++ b/samples/subcommands.cpp @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) { try { program.parse_args(argc, argv); - } catch (const std::runtime_error &err) { + } catch (const std::exception &err) { std::cerr << err.what() << std::endl; std::cerr << program; return 1;