diff options
author | Jelle Raaijmakers <jelle@gmta.nl> | 2021-06-07 21:20:35 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-08 11:30:58 +0200 |
commit | d7126fbbc2b03d005370ffb8d1195c6d11f11ec9 (patch) | |
tree | 8cc2d96cc33f97aa8fd62b38509402e1a2bd8de9 /Userland/Libraries | |
parent | 0b0bce78f60c54bd9d91dedc37258a6d75cf5053 (diff) | |
download | serenity-d7126fbbc2b03d005370ffb8d1195c6d11f11ec9.zip |
LibCore/ArgsParser: Learn how to stop on first non-option
We need this for utilities like `env`, that do not gain anything by
parsing the options passed to the command they are supposed to
execute.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/ArgsParser.cpp | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/ArgsParser.h | 2 |
2 files changed, 5 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp index 5dbec84b7e..c858bea4f0 100644 --- a/Userland/Libraries/LibCore/ArgsParser.cpp +++ b/Userland/Libraries/LibCore/ArgsParser.cpp @@ -42,6 +42,9 @@ bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_beha Vector<option> long_options; StringBuilder short_options_builder; + if (m_stop_on_first_non_option) + short_options_builder.append('+'); + int index_of_found_long_option = -1; // Tell getopt() to reset its internal state, and start scanning from optind = 1. diff --git a/Userland/Libraries/LibCore/ArgsParser.h b/Userland/Libraries/LibCore/ArgsParser.h index 4e88e8af06..42a082ece8 100644 --- a/Userland/Libraries/LibCore/ArgsParser.h +++ b/Userland/Libraries/LibCore/ArgsParser.h @@ -56,6 +56,7 @@ public: 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 set_stop_on_first_non_option(bool stop_on_first_non_option) { m_stop_on_first_non_option = stop_on_first_non_option; } void print_usage(FILE*, const char* argv0); void add_option(Option&&); @@ -81,6 +82,7 @@ private: bool m_show_help { false }; const char* m_general_help { nullptr }; + bool m_stop_on_first_non_option { false }; }; } |