mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 05:20:30 +00:00
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:
parent
190111e21a
commit
77dfd419e9
Notes:
sideshowbarker
2024-07-19 14:03:52 +09:00
Author: https://github.com/rburchell Commit: https://github.com/SerenityOS/serenity/commit/77dfd419e9b Pull-request: https://github.com/SerenityOS/serenity/pull/50
10 changed files with 49 additions and 56 deletions
|
@ -78,8 +78,7 @@ AK_OBJS = \
|
|||
../AK/StringBuilder.o \
|
||||
../AK/StringView.o \
|
||||
../AK/FileSystemPath.o \
|
||||
../AK/StdLibExtras.o \
|
||||
../AK/ArgsParser.o
|
||||
../AK/StdLibExtras.o
|
||||
|
||||
CXX_OBJS = $(KERNEL_OBJS) $(VFS_OBJS) $(AK_OBJS)
|
||||
OBJS = $(CXX_OBJS) Boot/boot.ao
|
||||
|
|
|
@ -7,8 +7,7 @@ AK_OBJS = \
|
|||
../AK/StringBuilder.o \
|
||||
../AK/FileSystemPath.o \
|
||||
../AK/StdLibExtras.o \
|
||||
../AK/MappedFile.o \
|
||||
../AK/ArgsParser.o
|
||||
../AK/MappedFile.o
|
||||
|
||||
LIBC_OBJS = \
|
||||
SharedBuffer.o \
|
||||
|
|
|
@ -1,40 +1,38 @@
|
|||
#include "ArgsParser.h"
|
||||
#include "StringBuilder.h"
|
||||
#include "CArgsParser.h"
|
||||
#include <AK/StringBuilder.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
bool ArgsParserResult::is_present(const String& arg_name) const
|
||||
bool CArgsParserResult::is_present(const String& arg_name) const
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const Vector<String>& ArgsParserResult::get_single_values() const
|
||||
const Vector<String>& CArgsParserResult::get_single_values() const
|
||||
{
|
||||
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)
|
||||
{}
|
||||
|
||||
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)
|
||||
{}
|
||||
|
||||
ArgsParser::ArgsParser(const String& program_name)
|
||||
CArgsParser::CArgsParser(const String& program_name)
|
||||
: 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
|
||||
if (argc < 2)
|
||||
|
@ -50,7 +48,7 @@ ArgsParserResult ArgsParser::parse(const int argc, const char** argv)
|
|||
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)
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool ArgsParser::check_required_args(const ArgsParserResult& res)
|
||||
bool CArgsParser::check_required_args(const CArgsParserResult& res)
|
||||
{
|
||||
for (auto& it : m_args) {
|
||||
if (it.value.required) {
|
||||
|
@ -128,32 +126,32 @@ bool ArgsParser::check_required_args(const ArgsParserResult& res)
|
|||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
void ArgsParser::add_single_value(const String& name)
|
||||
void CArgsParser::add_single_value(const String& name)
|
||||
{
|
||||
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) {
|
||||
// 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});
|
||||
}
|
||||
|
||||
String ArgsParser::get_usage() const
|
||||
String CArgsParser::get_usage() const
|
||||
{
|
||||
StringBuilder sb;
|
||||
|
||||
|
@ -224,9 +222,8 @@ String ArgsParser::get_usage() const
|
|||
return sb.to_string();
|
||||
}
|
||||
|
||||
void ArgsParser::print_usage() const
|
||||
void CArgsParser::print_usage() const
|
||||
{
|
||||
printf("%s\n", get_usage().characters());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include "AKString.h"
|
||||
#include "HashMap.h"
|
||||
#include "Vector.h"
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
namespace AK {
|
||||
|
||||
class ArgsParserResult {
|
||||
class CArgsParserResult {
|
||||
public:
|
||||
bool is_present(const String& arg_name) const;
|
||||
String get(const String& arg_name) const;
|
||||
|
@ -25,14 +23,14 @@ private:
|
|||
HashMap<String, String> m_args;
|
||||
Vector<String> m_single_values;
|
||||
|
||||
friend class ArgsParser;
|
||||
friend class CArgsParser;
|
||||
};
|
||||
|
||||
class ArgsParser {
|
||||
class CArgsParser {
|
||||
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& value_name, const String& description);
|
||||
|
@ -55,9 +53,9 @@ private:
|
|||
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 check_required_args(const ArgsParserResult& res);
|
||||
bool check_required_args(const CArgsParserResult& res);
|
||||
|
||||
String m_program_name;
|
||||
String m_prefix;
|
||||
|
@ -70,4 +68,3 @@ private:
|
|||
HashMap<String, Arg> m_args;
|
||||
};
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
include ../Makefile.common
|
||||
|
||||
OBJS = \
|
||||
CArgsParser.o \
|
||||
CIODevice.o \
|
||||
CFile.o \
|
||||
CSocket.o \
|
||||
|
|
|
@ -2,17 +2,17 @@
|
|||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <AK/ArgsParser.h>
|
||||
#include <LibCore/CArgsParser.h>
|
||||
|
||||
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_required_single_value("target");
|
||||
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();
|
||||
if (values.size() == 0) {
|
||||
args_parser.print_usage();
|
||||
|
|
|
@ -6,10 +6,10 @@
|
|||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/ArgsParser.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <AK/FileSystemPath.h>
|
||||
#include <LibCore/CArgsParser.h>
|
||||
#include <LibGUI/GDesktop.h>
|
||||
#include <LibGUI/GApplication.h>
|
||||
|
||||
|
@ -52,13 +52,13 @@ int main(int argc, char** 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("c", "show current wallpaper");
|
||||
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"))
|
||||
return handle_show_all();
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <LibCore/CProcessStatisticsReader.h>
|
||||
#include <LibCore/CArgsParser.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <AK/ArgsParser.h>
|
||||
#include <AK/HashMap.h>
|
||||
|
||||
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)
|
||||
{
|
||||
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("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 o_arg = args.is_present("o");
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <AK/ArgsParser.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/CArgsParser.h>
|
||||
|
||||
static String read_var(const String& name)
|
||||
{
|
||||
|
@ -104,12 +104,12 @@ static int handle_var(const String& var)
|
|||
|
||||
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_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")) {
|
||||
return handle_show_all();
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/ArgsParser.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <LibCore/CArgsParser.h>
|
||||
|
||||
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[])
|
||||
{
|
||||
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("n", "lines", "fetch the specified number of lines");
|
||||
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();
|
||||
if (values.size() != 1) {
|
||||
|
|
Loading…
Reference in a new issue