From 729507f2bde0a9ec4324f51c68c5646d593605f8 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Fri, 17 May 2019 12:51:44 +0200 Subject: [PATCH] ArgsParser: Remove boolean trap on add_arg Rather than requiring a boolean for whether or not the argument is required, add some new methods to make the purpose of the bool explicit. --- AK/ArgsParser.cpp | 18 ++++++++++++++---- AK/ArgsParser.h | 6 ++++-- Userland/pidof.cpp | 8 ++++---- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/AK/ArgsParser.cpp b/AK/ArgsParser.cpp index bba240aaa3..d98fffcea3 100644 --- a/AK/ArgsParser.cpp +++ b/AK/ArgsParser.cpp @@ -115,14 +115,24 @@ bool ArgsParser::check_required_args(const ArgsParserResult& res) return true; } -void ArgsParser::add_arg(const String& name, const String& description, bool required) +void ArgsParser::add_required_arg(const String& name, const String& description) { - m_args.set(name, Arg(name, description, required)); + m_args.set(name, Arg(name, description, true)); } -void ArgsParser::add_arg(const String& name, const String& value_name, const String& description, bool required) +void ArgsParser::add_required_arg(const String& name, const String& value_name, const String& description) { - m_args.set(name, Arg(name, value_name, description, required)); + m_args.set(name, Arg(name, value_name, description, true)); +} + +void ArgsParser::add_arg(const String& name, const String& description) +{ + m_args.set(name, Arg(name, description, false)); +} + +void ArgsParser::add_arg(const String& name, const String& value_name, const String& description) +{ + m_args.set(name, Arg(name, value_name, description, false)); } String ArgsParser::get_usage() const diff --git a/AK/ArgsParser.h b/AK/ArgsParser.h index 07c8e1593e..856e1ed976 100644 --- a/AK/ArgsParser.h +++ b/AK/ArgsParser.h @@ -34,8 +34,10 @@ public: ArgsParserResult parse(const int argc, const char** argv); - void add_arg(const String& name, const String& description, bool required); - void add_arg(const String& name, const String& value_name, const String& description, bool required); + void add_required_arg(const String& name, const String& description); + 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); String get_usage() const; void print_usage() const; diff --git a/Userland/pidof.cpp b/Userland/pidof.cpp index 833930464c..a0909cb88d 100644 --- a/Userland/pidof.cpp +++ b/Userland/pidof.cpp @@ -35,10 +35,10 @@ static int pid_of(const String& process_name, bool single_shot, bool omit_pid, p int main(int argc, char** argv) { AK::ArgsParser args_parser("pidof", "-"); - - args_parser.add_arg("s", "Single shot - this instructs the program to only return one pid", false); - args_parser.add_arg("o", "pid", "Tells pidof to omit processes with that pid. The special pid %PPID can be used to name the parent process of the pidof program.", false); - + + args_parser.add_arg("s", "Single shot - this instructs the program to only return one pid"); + args_parser.add_arg("o", "pid", "Tells pidof to omit processes with that pid. The special pid %PPID can be used to name the parent process of the pidof program."); + AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv); bool s_arg = args.is_present("s");