summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2022-07-12 23:05:06 +0200
committerLinus Groh <mail@linusgroh.de>2022-07-14 00:24:24 +0100
commita85b7287685ca63238584848adf48692dc5fe929 (patch)
treeddcc8901ef843791324e5c94c62fc251b7aa4ebe
parent3d516420378d5015b21c3500e910c1337935ea89 (diff)
downloadserenity-a85b7287685ca63238584848adf48692dc5fe929.zip
LibCore: Introduce support for optional ArgsParser option values
-rw-r--r--Userland/Libraries/LibCore/ArgsParser.cpp25
-rw-r--r--Userland/Libraries/LibCore/ArgsParser.h1
2 files changed, 19 insertions, 7 deletions
diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp
index b26bf87863..f7a82bd0aa 100644
--- a/Userland/Libraries/LibCore/ArgsParser.cpp
+++ b/Userland/Libraries/LibCore/ArgsParser.cpp
@@ -60,7 +60,7 @@ bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_beha
if (opt.long_name) {
option long_opt {
opt.long_name,
- opt.argument_mode == OptionArgumentMode::Required ? required_argument : no_argument,
+ opt.argument_mode == OptionArgumentMode::Required ? required_argument : (opt.argument_mode == OptionArgumentMode::Optional ? optional_argument : no_argument),
&index_of_found_long_option,
static_cast<int>(i)
};
@@ -70,6 +70,9 @@ bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_beha
short_options_builder.append(opt.short_name);
if (opt.argument_mode != OptionArgumentMode::None)
short_options_builder.append(':');
+ // Note: This is a GNU extension.
+ if (opt.argument_mode == OptionArgumentMode::Optional)
+ short_options_builder.append(':');
}
}
long_options.append({ 0, 0, 0, 0 });
@@ -202,6 +205,8 @@ void ArgsParser::print_usage_terminal(FILE* file, char const* argv0)
continue;
if (opt.argument_mode == OptionArgumentMode::Required)
out(file, " [{} {}]", opt.name_for_display(), opt.value_name);
+ else if (opt.argument_mode == OptionArgumentMode::Optional)
+ out(file, " [{}[{}{}]]", opt.name_for_display(), opt.long_name ? "="sv : ""sv, opt.value_name);
else
out(file, " [{}]", opt.name_for_display());
}
@@ -231,22 +236,24 @@ void ArgsParser::print_usage_terminal(FILE* file, char const* argv0)
if (opt.hide_mode == OptionHideMode::CommandLineAndMarkdown)
continue;
- auto print_argument = [&]() {
+ auto print_argument = [&](StringView value_delimiter) {
if (opt.value_name) {
if (opt.argument_mode == OptionArgumentMode::Required)
out(file, " {}", opt.value_name);
+ if (opt.argument_mode == OptionArgumentMode::Optional)
+ out(file, "[{}{}]", value_delimiter, opt.value_name);
}
};
out(file, "\t");
if (opt.short_name) {
out(file, "\033[1m-{}\033[0m", opt.short_name);
- print_argument();
+ print_argument(""sv);
}
if (opt.short_name && opt.long_name)
out(file, ", ");
if (opt.long_name) {
out(file, "\033[1m--{}\033[0m", opt.long_name);
- print_argument();
+ print_argument("="sv);
}
if (opt.help_string)
@@ -279,6 +286,8 @@ void ArgsParser::print_usage_markdown(FILE* file, char const* argv0)
// currently display a blank name after the option.
if (opt.argument_mode == OptionArgumentMode::Required)
out(file, " [{} {}]", opt.name_for_display(), opt.value_name ?: "");
+ else if (opt.argument_mode == OptionArgumentMode::Optional)
+ out(file, " [{}[{}{}]]", opt.name_for_display(), opt.long_name ? "="sv : ""sv, opt.value_name);
else
out(file, " [{}]", opt.name_for_display());
}
@@ -318,23 +327,25 @@ void ArgsParser::print_usage_markdown(FILE* file, char const* argv0)
if (!should_display_option(opt))
continue;
- auto print_argument = [&]() {
+ auto print_argument = [&](StringView value_delimiter) {
if (opt.value_name != nullptr) {
if (opt.argument_mode == OptionArgumentMode::Required)
out(file, " {}", opt.value_name);
+ if (opt.argument_mode == OptionArgumentMode::Optional)
+ out(file, "[{}{}]", value_delimiter, opt.value_name);
}
};
out(file, "* ");
if (opt.short_name != '\0') {
out(file, "`-{}", opt.short_name);
- print_argument();
+ print_argument(""sv);
out(file, "`");
}
if (opt.short_name != '\0' && opt.long_name != nullptr)
out(file, ", ");
if (opt.long_name != nullptr) {
out(file, "`--{}", opt.long_name);
- print_argument();
+ print_argument("="sv);
out(file, "`");
}
diff --git a/Userland/Libraries/LibCore/ArgsParser.h b/Userland/Libraries/LibCore/ArgsParser.h
index 68fbbc4b77..fb8a191164 100644
--- a/Userland/Libraries/LibCore/ArgsParser.h
+++ b/Userland/Libraries/LibCore/ArgsParser.h
@@ -32,6 +32,7 @@ public:
enum class OptionArgumentMode {
None,
+ Optional,
Required,
};