summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Burchell <robin+git@viroteck.net>2019-05-17 15:17:43 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-05-17 15:49:37 +0200
commitbffed9e3cba28f53c88801e4857a20abbfd20b48 (patch)
treea53c0de0b3aae0296e5cd1dd33d5edeaf77c6183
parentb92fa59832bd69d52ccef045031b66521ab7a5ed (diff)
downloadserenity-bffed9e3cba28f53c88801e4857a20abbfd20b48.zip
ArgsParser: Expand to be able to handle multiple single arguments
This is needed for e.g. ln
-rw-r--r--AK/ArgsParser.cpp37
-rw-r--r--AK/ArgsParser.h12
-rw-r--r--Userland/pape.cpp2
-rw-r--r--Userland/sysctl.cpp2
-rw-r--r--Userland/tail.cpp2
5 files changed, 34 insertions, 21 deletions
diff --git a/AK/ArgsParser.cpp b/AK/ArgsParser.cpp
index 0bd4112013..ec796c1972 100644
--- a/AK/ArgsParser.cpp
+++ b/AK/ArgsParser.cpp
@@ -113,8 +113,15 @@ bool ArgsParser::check_required_args(const ArgsParserResult& res)
}
}
- if (m_single_value_required) {
- if (res.m_single_values.size() == 0)
+ int required_arguments = 0;
+ for (const auto& a : m_single_args) {
+ if (a.required) {
+ required_arguments++;
+ }
+ }
+
+ if (required_arguments != 0) {
+ if (res.m_single_values.size() < required_arguments)
return false;
}
@@ -141,16 +148,18 @@ void ArgsParser::add_arg(const String& name, const String& value_name, const Str
m_args.set(name, Arg(name, value_name, description, false));
}
-void ArgsParser::set_single_value(const String& name)
+void ArgsParser::add_single_value(const String& name)
{
- m_single_value_name = name;
- m_single_value_required = false;
+ m_single_args.append(SingleArg{name, false});
}
-void ArgsParser::set_required_single_value(const String& name)
+void ArgsParser::add_required_single_value(const String& name)
{
- m_single_value_name = name;
- m_single_value_required = true;
+ if (m_single_args.size() != 0) {
+ // adding required arguments after non-required arguments would be nonsensical
+ ASSERT(m_single_args.last().required);
+ }
+ m_single_args.append(SingleArg{name, true});
}
String ArgsParser::get_usage() const
@@ -181,18 +190,18 @@ String ArgsParser::get_usage() const
sb.append("] ");
}
- if (m_single_value_name.length()) {
- if (m_single_value_required)
+ for (auto& arg : m_single_args) {
+ if (arg.required)
sb.append("<");
else
sb.append("[");
- sb.append(m_single_value_name);
+ sb.append(arg.name);
- if (m_single_value_required)
- sb.append(">");
+ if (arg.required)
+ sb.append("> ");
else
- sb.append("]");
+ sb.append("] ");
}
sb.append("\n");
diff --git a/AK/ArgsParser.h b/AK/ArgsParser.h
index 53eebc8127..b0af7b1988 100644
--- a/AK/ArgsParser.h
+++ b/AK/ArgsParser.h
@@ -38,8 +38,8 @@ public:
void add_required_arg(const String& name, const String& value_name, const String& description);
void add_arg(const String& name, const String& description);
void add_arg(const String& name, const String& value_name, const String& description);
- void set_single_value(const String& name);
- void set_required_single_value(const String& name);
+ void add_single_value(const String& name);
+ void add_required_single_value(const String& name);
String get_usage() const;
void print_usage() const;
@@ -61,8 +61,12 @@ private:
String m_program_name;
String m_prefix;
- String m_single_value_name;
- bool m_single_value_required = false;
+
+ struct SingleArg {
+ String name;
+ bool required;
+ };
+ Vector<SingleArg> m_single_args;
HashMap<String, Arg> m_args;
};
diff --git a/Userland/pape.cpp b/Userland/pape.cpp
index 0458d84c07..395007d6f5 100644
--- a/Userland/pape.cpp
+++ b/Userland/pape.cpp
@@ -56,7 +56,7 @@ int main(int argc, char** argv)
args_parser.add_arg("a", "show all wallpapers");
args_parser.add_arg("c", "show current wallpaper");
- args_parser.set_single_value("name");
+ args_parser.add_single_value("name");
AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv);
diff --git a/Userland/sysctl.cpp b/Userland/sysctl.cpp
index 7bc2d14d01..60d5b04aba 100644
--- a/Userland/sysctl.cpp
+++ b/Userland/sysctl.cpp
@@ -107,7 +107,7 @@ int main(int argc, char** argv)
AK::ArgsParser args_parser("sysctl");
args_parser.add_arg("a", "show all variables");
- args_parser.set_single_value("variable=[value]");
+ args_parser.add_single_value("variable=[value]");
AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv);
diff --git a/Userland/tail.cpp b/Userland/tail.cpp
index 46e0e0a214..ff416e3843 100644
--- a/Userland/tail.cpp
+++ b/Userland/tail.cpp
@@ -79,7 +79,7 @@ int main(int argc, char *argv[])
args_parser.add_arg("f", "follow -- appended data is output as it is written to the file");
args_parser.add_arg("n", "lines", "fetch the specified number of lines");
- args_parser.set_required_single_value("file");
+ args_parser.add_required_single_value("file");
AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv);