mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:27:45 +00:00
LibCore: Make ArgsParser perform some completion if passed --complete
This makes it possible to autocomplete flags and options via the Shell.
This commit is contained in:
parent
e6bd1f8807
commit
fc4d36ccd0
2 changed files with 147 additions and 28 deletions
|
@ -32,11 +32,11 @@ public:
|
|||
|
||||
struct Option {
|
||||
bool requires_argument { true };
|
||||
const char* help_string { nullptr };
|
||||
const char* long_name { nullptr };
|
||||
char const* help_string { nullptr };
|
||||
char const* long_name { nullptr };
|
||||
char short_name { 0 };
|
||||
const char* value_name { nullptr };
|
||||
Function<bool(const char*)> accept_value;
|
||||
char const* value_name { nullptr };
|
||||
Function<bool(char const*)> accept_value;
|
||||
|
||||
String name_for_display() const
|
||||
{
|
||||
|
@ -47,11 +47,11 @@ public:
|
|||
};
|
||||
|
||||
struct Arg {
|
||||
const char* help_string { nullptr };
|
||||
const char* name { nullptr };
|
||||
char const* help_string { nullptr };
|
||||
char const* name { nullptr };
|
||||
int min_values { 0 };
|
||||
int max_values { 1 };
|
||||
Function<bool(const char*)> accept_value;
|
||||
Function<bool(char const*)> accept_value;
|
||||
};
|
||||
|
||||
bool parse(int argc, char* const* argv, FailureBehavior failure_behavior = FailureBehavior::PrintUsageAndExit);
|
||||
|
@ -61,42 +61,45 @@ public:
|
|||
}
|
||||
|
||||
// *Without* trailing newline!
|
||||
void set_general_help(const char* help_string) { m_general_help = help_string; };
|
||||
void set_general_help(char const* 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 print_usage_terminal(FILE*, const char* argv0);
|
||||
void print_usage_markdown(FILE*, const char* argv0);
|
||||
void print_usage(FILE*, char const* argv0);
|
||||
void print_usage_terminal(FILE*, char const* argv0);
|
||||
void print_usage_markdown(FILE*, char const* argv0);
|
||||
void print_version(FILE*);
|
||||
|
||||
void add_option(Option&&);
|
||||
void add_ignored(const char* long_name, char short_name);
|
||||
void add_option(bool& value, const char* help_string, const char* long_name, char short_name);
|
||||
void add_option(const char*& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||
void add_option(String& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||
void add_ignored(char const* long_name, char short_name);
|
||||
void add_option(bool& value, char const* help_string, char const* long_name, char short_name);
|
||||
void add_option(char const*& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||
void add_option(String& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||
void add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||
void add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||
void add_option(unsigned& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||
void add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||
void add_option(Optional<double>& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||
void add_option(Optional<size_t>& value, const char* help_string, const char* long_name, char short_name, const char* value_name);
|
||||
void add_option(int& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||
void add_option(unsigned& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||
void add_option(double& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||
void add_option(Optional<double>& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||
void add_option(Optional<size_t>& value, char const* help_string, char const* long_name, char short_name, char const* value_name);
|
||||
|
||||
void add_positional_argument(Arg&&);
|
||||
void add_positional_argument(const char*& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||
void add_positional_argument(String& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||
void add_positional_argument(char const*& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(String& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(StringView& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(int& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||
void add_positional_argument(unsigned& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||
void add_positional_argument(double& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||
void add_positional_argument(Vector<const char*>& value, const char* help_string, const char* name, Required required = Required::Yes);
|
||||
void add_positional_argument(int& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(unsigned& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(double& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(Vector<char const*>& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(Vector<StringView>& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
|
||||
private:
|
||||
void autocomplete(FILE*, StringView program_name, Span<char const* const> remaining_arguments);
|
||||
|
||||
Vector<Option> m_options;
|
||||
Vector<Arg> m_positional_args;
|
||||
|
||||
bool m_show_help { false };
|
||||
bool m_show_version { false };
|
||||
const char* m_general_help { nullptr };
|
||||
bool m_perform_autocomplete { false };
|
||||
char const* m_general_help { nullptr };
|
||||
bool m_stop_on_first_non_option { false };
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue