1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:07:34 +00:00

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.
This commit is contained in:
Jelle Raaijmakers 2021-06-07 21:20:35 +02:00 committed by Andreas Kling
parent 0b0bce78f6
commit d7126fbbc2
3 changed files with 181 additions and 0 deletions

View file

@ -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.

View file

@ -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 };
};
}