LibCore: Move AK/ArgsParser to LibCore/CArgsParser

Also rename the classes to match LibCore naming style.
This means that it's no longer incorrectly linked into LibC and Kernel.
This commit is contained in:
Robin Burchell 2019-05-17 15:35:30 +02:00 committed by Andreas Kling
parent 190111e21a
commit 77dfd419e9
10 changed files with 49 additions and 56 deletions

View file

@ -78,8 +78,7 @@ AK_OBJS = \
../AK/StringBuilder.o \ ../AK/StringBuilder.o \
../AK/StringView.o \ ../AK/StringView.o \
../AK/FileSystemPath.o \ ../AK/FileSystemPath.o \
../AK/StdLibExtras.o \ ../AK/StdLibExtras.o
../AK/ArgsParser.o
CXX_OBJS = $(KERNEL_OBJS) $(VFS_OBJS) $(AK_OBJS) CXX_OBJS = $(KERNEL_OBJS) $(VFS_OBJS) $(AK_OBJS)
OBJS = $(CXX_OBJS) Boot/boot.ao OBJS = $(CXX_OBJS) Boot/boot.ao

View file

@ -7,8 +7,7 @@ AK_OBJS = \
../AK/StringBuilder.o \ ../AK/StringBuilder.o \
../AK/FileSystemPath.o \ ../AK/FileSystemPath.o \
../AK/StdLibExtras.o \ ../AK/StdLibExtras.o \
../AK/MappedFile.o \ ../AK/MappedFile.o
../AK/ArgsParser.o
LIBC_OBJS = \ LIBC_OBJS = \
SharedBuffer.o \ SharedBuffer.o \

View file

@ -1,40 +1,38 @@
#include "ArgsParser.h" #include "CArgsParser.h"
#include "StringBuilder.h" #include <AK/StringBuilder.h>
#include <stdio.h> #include <stdio.h>
namespace AK { bool CArgsParserResult::is_present(const String& arg_name) const
bool ArgsParserResult::is_present(const String& arg_name) const
{ {
return m_args.contains(arg_name); return m_args.contains(arg_name);
} }
String ArgsParserResult::get(const String& arg_name) const String CArgsParserResult::get(const String& arg_name) const
{ {
return m_args.get(arg_name); return m_args.get(arg_name);
} }
const Vector<String>& ArgsParserResult::get_single_values() const const Vector<String>& CArgsParserResult::get_single_values() const
{ {
return m_single_values; return m_single_values;
} }
ArgsParser::Arg::Arg(const String& name, const String& description, bool required) CArgsParser::Arg::Arg(const String& name, const String& description, bool required)
: name(name), description(description), required(required) : name(name), description(description), required(required)
{} {}
ArgsParser::Arg::Arg(const String& name, const String& value_name, const String& description, bool required) CArgsParser::Arg::Arg(const String& name, const String& value_name, const String& description, bool required)
: name(name), description(description), value_name(value_name), required(required) : name(name), description(description), value_name(value_name), required(required)
{} {}
ArgsParser::ArgsParser(const String& program_name) CArgsParser::CArgsParser(const String& program_name)
: m_program_name(program_name), m_prefix("-") : m_program_name(program_name), m_prefix("-")
{} {}
ArgsParserResult ArgsParser::parse(const int argc, const char** argv) CArgsParserResult CArgsParser::parse(const int argc, const char** argv)
{ {
ArgsParserResult res; CArgsParserResult res;
// We should have at least one parameter // We should have at least one parameter
if (argc < 2) if (argc < 2)
@ -50,7 +48,7 @@ ArgsParserResult ArgsParser::parse(const int argc, const char** argv)
return res; return res;
} }
int ArgsParser::parse_next_param(const int index, const char** argv, const int params_left, ArgsParserResult& res) int CArgsParser::parse_next_param(const int index, const char** argv, const int params_left, CArgsParserResult& res)
{ {
if (params_left == 0) if (params_left == 0)
return 0; return 0;
@ -99,12 +97,12 @@ int ArgsParser::parse_next_param(const int index, const char** argv, const int p
return parse_next_param(index + 1, argv, params_left - 1, res); return parse_next_param(index + 1, argv, params_left - 1, res);
} }
bool ArgsParser::is_param_valid(const String& param_name) bool CArgsParser::is_param_valid(const String& param_name)
{ {
return param_name.substring(0, m_prefix.length()) == m_prefix; return param_name.substring(0, m_prefix.length()) == m_prefix;
} }
bool ArgsParser::check_required_args(const ArgsParserResult& res) bool CArgsParser::check_required_args(const CArgsParserResult& res)
{ {
for (auto& it : m_args) { for (auto& it : m_args) {
if (it.value.required) { if (it.value.required) {
@ -128,32 +126,32 @@ bool ArgsParser::check_required_args(const ArgsParserResult& res)
return true; return true;
} }
void ArgsParser::add_required_arg(const String& name, const String& description) void CArgsParser::add_required_arg(const String& name, const String& description)
{ {
m_args.set(name, Arg(name, description, true)); m_args.set(name, Arg(name, description, true));
} }
void ArgsParser::add_required_arg(const String& name, const String& value_name, const String& description) void CArgsParser::add_required_arg(const String& name, const String& value_name, const String& description)
{ {
m_args.set(name, Arg(name, value_name, description, true)); m_args.set(name, Arg(name, value_name, description, true));
} }
void ArgsParser::add_arg(const String& name, const String& description) void CArgsParser::add_arg(const String& name, const String& description)
{ {
m_args.set(name, Arg(name, description, false)); m_args.set(name, Arg(name, description, false));
} }
void ArgsParser::add_arg(const String& name, const String& value_name, const String& description) void CArgsParser::add_arg(const String& name, const String& value_name, const String& description)
{ {
m_args.set(name, Arg(name, value_name, description, false)); m_args.set(name, Arg(name, value_name, description, false));
} }
void ArgsParser::add_single_value(const String& name) void CArgsParser::add_single_value(const String& name)
{ {
m_single_args.append(SingleArg{name, false}); m_single_args.append(SingleArg{name, false});
} }
void ArgsParser::add_required_single_value(const String& name) void CArgsParser::add_required_single_value(const String& name)
{ {
if (m_single_args.size() != 0) { if (m_single_args.size() != 0) {
// adding required arguments after non-required arguments would be nonsensical // adding required arguments after non-required arguments would be nonsensical
@ -162,7 +160,7 @@ void ArgsParser::add_required_single_value(const String& name)
m_single_args.append(SingleArg{name, true}); m_single_args.append(SingleArg{name, true});
} }
String ArgsParser::get_usage() const String CArgsParser::get_usage() const
{ {
StringBuilder sb; StringBuilder sb;
@ -224,9 +222,8 @@ String ArgsParser::get_usage() const
return sb.to_string(); return sb.to_string();
} }
void ArgsParser::print_usage() const void CArgsParser::print_usage() const
{ {
printf("%s\n", get_usage().characters()); printf("%s\n", get_usage().characters());
} }
}

View file

@ -1,8 +1,8 @@
#pragma once #pragma once
#include "AKString.h" #include <AK/AKString.h>
#include "HashMap.h" #include <AK/HashMap.h>
#include "Vector.h" #include <AK/Vector.h>
/* /*
The class ArgsParser provides a way to parse arguments by using a given list that describes the possible The class ArgsParser provides a way to parse arguments by using a given list that describes the possible
@ -13,9 +13,7 @@
retrieve its value...). In case of error (missing required argument) an empty structure is returned as result. retrieve its value...). In case of error (missing required argument) an empty structure is returned as result.
*/ */
namespace AK { class CArgsParserResult {
class ArgsParserResult {
public: public:
bool is_present(const String& arg_name) const; bool is_present(const String& arg_name) const;
String get(const String& arg_name) const; String get(const String& arg_name) const;
@ -25,14 +23,14 @@ private:
HashMap<String, String> m_args; HashMap<String, String> m_args;
Vector<String> m_single_values; Vector<String> m_single_values;
friend class ArgsParser; friend class CArgsParser;
}; };
class ArgsParser { class CArgsParser {
public: public:
ArgsParser(const String& program_name); CArgsParser(const String& program_name);
ArgsParserResult parse(const int argc, const char** argv); CArgsParserResult parse(const int argc, const char** argv);
void add_required_arg(const String& name, const String& description); 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_required_arg(const String& name, const String& value_name, const String& description);
@ -55,9 +53,9 @@ private:
bool required; bool required;
}; };
int parse_next_param(const int index, const char** argv, const int params_left, ArgsParserResult& res); int parse_next_param(const int index, const char** argv, const int params_left, CArgsParserResult& res);
bool is_param_valid(const String& param_name); bool is_param_valid(const String& param_name);
bool check_required_args(const ArgsParserResult& res); bool check_required_args(const CArgsParserResult& res);
String m_program_name; String m_program_name;
String m_prefix; String m_prefix;
@ -70,4 +68,3 @@ private:
HashMap<String, Arg> m_args; HashMap<String, Arg> m_args;
}; };
}

View file

@ -1,6 +1,7 @@
include ../Makefile.common include ../Makefile.common
OBJS = \ OBJS = \
CArgsParser.o \
CIODevice.o \ CIODevice.o \
CFile.o \ CFile.o \
CSocket.o \ CSocket.o \

View file

@ -2,17 +2,17 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <AK/ArgsParser.h> #include <LibCore/CArgsParser.h>
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
AK::ArgsParser args_parser("ln"); CArgsParser args_parser("ln");
args_parser.add_arg("s", "create a symlink"); args_parser.add_arg("s", "create a symlink");
args_parser.add_required_single_value("target"); args_parser.add_required_single_value("target");
args_parser.add_required_single_value("link-path"); args_parser.add_required_single_value("link-path");
AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv); CArgsParserResult args = args_parser.parse(argc, (const char**)argv);
Vector<String> values = args.get_single_values(); Vector<String> values = args.get_single_values();
if (values.size() == 0) { if (values.size() == 0) {
args_parser.print_usage(); args_parser.print_usage();

View file

@ -6,10 +6,10 @@
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h> #include <dirent.h>
#include <AK/AKString.h> #include <AK/AKString.h>
#include <AK/ArgsParser.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <AK/FileSystemPath.h> #include <AK/FileSystemPath.h>
#include <LibCore/CArgsParser.h>
#include <LibGUI/GDesktop.h> #include <LibGUI/GDesktop.h>
#include <LibGUI/GApplication.h> #include <LibGUI/GApplication.h>
@ -52,13 +52,13 @@ int main(int argc, char** argv)
{ {
GApplication app(argc, argv); GApplication app(argc, argv);
AK::ArgsParser args_parser("pape"); CArgsParser args_parser("pape");
args_parser.add_arg("a", "show all wallpapers"); args_parser.add_arg("a", "show all wallpapers");
args_parser.add_arg("c", "show current wallpaper"); args_parser.add_arg("c", "show current wallpaper");
args_parser.add_single_value("name"); args_parser.add_single_value("name");
AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv); CArgsParserResult args = args_parser.parse(argc, (const char**)argv);
if (args.is_present("a")) if (args.is_present("a"))
return handle_show_all(); return handle_show_all();

View file

@ -3,9 +3,9 @@
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <LibCore/CProcessStatisticsReader.h> #include <LibCore/CProcessStatisticsReader.h>
#include <LibCore/CArgsParser.h>
#include <AK/AKString.h> #include <AK/AKString.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <AK/ArgsParser.h>
#include <AK/HashMap.h> #include <AK/HashMap.h>
static int pid_of(const String& process_name, bool single_shot, bool omit_pid, pid_t pid) static int pid_of(const String& process_name, bool single_shot, bool omit_pid, pid_t pid)
@ -34,12 +34,12 @@ static int pid_of(const String& process_name, bool single_shot, bool omit_pid, p
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
AK::ArgsParser args_parser("pidof"); CArgsParser args_parser("pidof");
args_parser.add_arg("s", "Single shot - this instructs the program to only return one pid"); 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."); 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); CArgsParserResult args = args_parser.parse(argc, (const char**)argv);
bool s_arg = args.is_present("s"); bool s_arg = args.is_present("s");
bool o_arg = args.is_present("o"); bool o_arg = args.is_present("o");

View file

@ -4,10 +4,10 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <AK/ArgsParser.h>
#include <AK/AKString.h> #include <AK/AKString.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/CArgsParser.h>
static String read_var(const String& name) static String read_var(const String& name)
{ {
@ -104,12 +104,12 @@ static int handle_var(const String& var)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
AK::ArgsParser args_parser("sysctl"); CArgsParser args_parser("sysctl");
args_parser.add_arg("a", "show all variables"); args_parser.add_arg("a", "show all variables");
args_parser.add_single_value("variable=[value]"); args_parser.add_single_value("variable=[value]");
AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv); CArgsParserResult args = args_parser.parse(argc, (const char**)argv);
if (args.is_present("a")) { if (args.is_present("a")) {
return handle_show_all(); return handle_show_all();

View file

@ -4,8 +4,8 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/ArgsParser.h>
#include <LibCore/CFile.h> #include <LibCore/CFile.h>
#include <LibCore/CArgsParser.h>
int tail_from_pos(CFile& file, off_t startline, bool want_follow) int tail_from_pos(CFile& file, off_t startline, bool want_follow)
{ {
@ -75,13 +75,13 @@ static void exit_because_we_wanted_lines()
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
AK::ArgsParser args_parser("tail"); CArgsParser args_parser("tail");
args_parser.add_arg("f", "follow -- appended data is output as it is written to the file"); 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.add_arg("n", "lines", "fetch the specified number of lines");
args_parser.add_required_single_value("file"); args_parser.add_required_single_value("file");
AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv); CArgsParserResult args = args_parser.parse(argc, (const char**)argv);
Vector<String> values = args.get_single_values(); Vector<String> values = args.get_single_values();
if (values.size() != 1) { if (values.size() != 1) {