summaryrefslogtreecommitdiff
path: root/Libraries/LibCore/CArgsParser.cpp
blob: d4d7751a46c909018228bfa9db4aa26e3fbcdad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "CArgsParser.h"
#include <AK/StringBuilder.h>

#include <stdio.h>

bool CArgsParserResult::is_present(const String& arg_name) const
{
    return m_args.contains(arg_name);
}

String CArgsParserResult::get(const String& arg_name) const
{
    return m_args.get(arg_name).value_or({});
}

const Vector<String>& CArgsParserResult::get_single_values() const
{
    return m_single_values;
}

CArgsParser::Arg::Arg(const String& name, const String& description, bool required)
    : name(name)
    , description(description)
    , required(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)
{
}

CArgsParser::CArgsParser(const String& program_name)
    : m_program_name(program_name)
    , m_prefix("-")
{
}

CArgsParserResult CArgsParser::parse(int argc, char** argv)
{
    CArgsParserResult res;

    // We should have at least one parameter
    if (argc < 2)
        return {};

    // We parse the first parameter at the index 1
    if (parse_next_param(1, argv, argc - 1, res) != 0)
        return {};

    if (!check_required_args(res))
        return {};

    return res;
}

int CArgsParser::parse_next_param(int index, char** argv, const int params_left, CArgsParserResult& res)
{
    if (params_left == 0)
        return 0;

    String param = argv[index];

    // We check if the prefix is found at the beginning of the param name
    if (is_param_valid(param)) {
        auto prefix_length = m_prefix.length();
        String param_name = param.substring(prefix_length, param.length() - prefix_length);

        auto arg = m_args.find(param_name);
        if (arg == m_args.end()) {
            printf("Unknown arg \"");
            if (!param_name.is_null())
                printf("%s", param_name.characters());
            printf("\"\n");
            return -1;
        }

        // If this parameter must be followed by a value, we look for it
        if (!arg->value.value_name.is_null()) {
            if (params_left < 1) {
                printf("Missing value for argument %s\n", arg->value.name.characters());
                return -1;
            }

            String next = String(argv[index + 1]);

            if (is_param_valid(next)) {
                printf("Missing value for argument %s\n", arg->value.name.characters());
                return -1;
            }

            res.m_args.set(arg->value.name, next);
            return parse_next_param(index + 2, argv, params_left - 2, res);
        }

        // Single argument, not followed by a value
        res.m_args.set(arg->value.name, "");
        return parse_next_param(index + 1, argv, params_left - 1, res);
    }

    // Else, it's a value alone, a file name parameter for example
    res.m_single_values.append(param);
    return parse_next_param(index + 1, argv, params_left - 1, res);
}

bool CArgsParser::is_param_valid(const String& param_name)
{
    return param_name.substring(0, m_prefix.length()) == m_prefix;
}

bool CArgsParser::check_required_args(const CArgsParserResult& res)
{
    for (auto& it : m_args) {
        if (it.value.required) {
            if (!res.is_present(it.value.name))
                return false;
        }
    }

    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;
    }

    return true;
}

void CArgsParser::add_required_arg(const String& name, const String& description)
{
    m_args.set(name, Arg(name, description, true));
}

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 CArgsParser::add_arg(const String& name, const String& description)
{
    m_args.set(name, Arg(name, description, false));
}

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 CArgsParser::add_single_value(const String& name)
{
    m_single_args.append(SingleArg { name, false });
}

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
        ASSERT(m_single_args.last().required);
    }
    m_single_args.append(SingleArg { name, true });
}

String CArgsParser::get_usage() const
{
    StringBuilder sb;

    sb.append("usage : ");
    sb.append(m_program_name);
    sb.append(" ");

    for (auto& it : m_args) {
        if (it.value.required)
            sb.append("<");
        else
            sb.append("[");

        sb.append(m_prefix);
        sb.append(it.value.name);

        if (!it.value.value_name.is_null()) {
            sb.append(" ");
            sb.append(it.value.value_name);
        }

        if (it.value.required)
            sb.append("> ");
        else
            sb.append("] ");
    }

    for (auto& arg : m_single_args) {
        if (arg.required)
            sb.append("<");
        else
            sb.append("[");

        sb.append(arg.name);

        if (arg.required)
            sb.append("> ");
        else
            sb.append("] ");
    }

    sb.append("\n");

    for (auto& it : m_args) {
        sb.append("    ");
        sb.append(m_prefix);
        sb.append(it.value.name);

        if (!it.value.value_name.is_null()) {
            sb.append(" ");
            sb.append(it.value.value_name);
        }

        sb.append(" : ");
        sb.append(it.value.description);
        sb.append("\n");
    }

    return sb.to_string();
}

void CArgsParser::print_usage() const
{
    printf("%s\n", get_usage().characters());
}