mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
ArgsParser: Expand to be able to handle multiple single arguments
This is needed for e.g. ln
This commit is contained in:
parent
b92fa59832
commit
bffed9e3cb
5 changed files with 34 additions and 21 deletions
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue