diff options
author | Jelle Raaijmakers <jelle@gmta.nl> | 2021-06-06 01:11:56 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-08 11:30:58 +0200 |
commit | 250f8eccf31313ab7f435f9bf119d90564d95eb6 (patch) | |
tree | faf39148c4993f9cd38b89e40077d1bb065f1d11 /Userland/Libraries | |
parent | 1dc31842cb1ac27e7ee180db1aa08e98e46527c6 (diff) | |
download | serenity-250f8eccf31313ab7f435f9bf119d90564d95eb6.zip |
LibCore: Support fine-grained failure behavior for ArgsParser
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/ArgsParser.cpp | 21 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/ArgsParser.h | 9 |
2 files changed, 19 insertions, 11 deletions
diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp index 3c8e3597a0..5dbec84b7e 100644 --- a/Userland/Libraries/LibCore/ArgsParser.cpp +++ b/Userland/Libraries/LibCore/ArgsParser.cpp @@ -30,11 +30,12 @@ ArgsParser::ArgsParser() add_option(m_show_help, "Display this message", "help", 0); } -bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure) +bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_behavior) { - auto print_usage_and_exit = [this, argv, exit_on_failure] { - print_usage(stderr, argv[0]); - if (exit_on_failure) + auto fail = [this, argv, failure_behavior] { + if (failure_behavior == FailureBehavior::PrintUsage || failure_behavior == FailureBehavior::PrintUsageAndExit) + print_usage(stderr, argv[0]); + if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit) exit(1); }; @@ -76,7 +77,7 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure) } else if (c == '?') { // There was an error, and getopt() has already // printed its error message. - print_usage_and_exit(); + fail(); return false; } @@ -98,7 +99,7 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure) const char* arg = found_option->requires_argument ? optarg : nullptr; if (!found_option->accept_value(arg)) { warnln("\033[31mInvalid value for option \033[1m{}\033[22m, dude\033[0m", found_option->name_for_display()); - print_usage_and_exit(); + fail(); return false; } } @@ -116,7 +117,7 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure) } if (total_values_required > values_left) { - print_usage_and_exit(); + fail(); return false; } int extra_values_to_distribute = values_left - total_values_required; @@ -132,7 +133,7 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure) if (extra_values_to_distribute > 0) { // We still have too many values :( - print_usage_and_exit(); + fail(); return false; } @@ -142,7 +143,7 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure) const char* value = argv[optind++]; if (!arg.accept_value(value)) { warnln("Invalid value for argument {}", arg.name); - print_usage_and_exit(); + fail(); return false; } } @@ -152,7 +153,7 @@ bool ArgsParser::parse(int argc, char** argv, bool exit_on_failure) // Now let's show help if requested. if (m_show_help) { print_usage(stdout, argv[0]); - if (exit_on_failure) + if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit) exit(0); return false; } diff --git a/Userland/Libraries/LibCore/ArgsParser.h b/Userland/Libraries/LibCore/ArgsParser.h index 5c73437064..4e88e8af06 100644 --- a/Userland/Libraries/LibCore/ArgsParser.h +++ b/Userland/Libraries/LibCore/ArgsParser.h @@ -22,6 +22,13 @@ public: No }; + enum class FailureBehavior { + PrintUsageAndExit, + PrintUsage, + Exit, + Ignore, + }; + struct Option { bool requires_argument { true }; const char* help_string { nullptr }; @@ -46,7 +53,7 @@ public: Function<bool(const char*)> accept_value; }; - bool parse(int argc, char** argv, bool exit_on_failure = true); + bool parse(int argc, char* const* argv, FailureBehavior failure_behavior = FailureBehavior::PrintUsageAndExit); // *Without* trailing newline! void set_general_help(const char* help_string) { m_general_help = help_string; }; void print_usage(FILE*, const char* argv0); |