File: session.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (316 lines) | stat: -rw-r--r-- 9,930 bytes parent folder | download
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.


#include <libdnf5-cli/session.hpp>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>


namespace libdnf5::cli::session {

class Session::Impl {
public:
    Impl() : argument_parser(new libdnf5::cli::ArgumentParser) {}

    void add_and_initialize_command(std::unique_ptr<Command> && command);

    Command * get_root_command() {
        auto * arg_parser_root_command = argument_parser->get_root_command();
        return arg_parser_root_command ? static_cast<Command *>(arg_parser_root_command->get_user_data()) : nullptr;
    }

    void set_root_command(Command & command) {
        argument_parser->set_root_command(command.get_argument_parser_command());
    }

    Command * get_selected_command() { return selected_command; }

    void set_selected_command(Command * command) { selected_command = command; }

    libdnf5::cli::ArgumentParser & get_argument_parser() { return *argument_parser; }

    void clear() {
        for (auto & cmd : commands) {
            cmd.reset();
        }
        argument_parser.reset();
    }

private:
    Command * selected_command{nullptr};
    std::vector<std::unique_ptr<Command>> commands;
    std::unique_ptr<libdnf5::cli::ArgumentParser> argument_parser;
};


void Session::Impl::add_and_initialize_command(std::unique_ptr<Command> && command) {
    auto * arg_parser_command = command->get_argument_parser_command();

    // set the command as selected when parsed
    arg_parser_command->set_parse_hook_func([](ArgumentParser::Argument * arg,
                                               [[maybe_unused]] const char * option,
                                               [[maybe_unused]] int argc,
                                               [[maybe_unused]] const char * const argv[]) {
        // set the selected command only if a subcommand hasn't set it already
        auto * command = static_cast<Command *>(arg->get_user_data());
        auto & session = command->get_session();
        if (!session.get_selected_command()) {
            session.set_selected_command(command);
        }
        return true;
    });

    command->set_parent_command();
    command->set_argument_parser();
    command->register_subcommands();
    commands.push_back(std::move(command));
}


Session::Session() : p_impl{new Impl} {}

Session::~Session() = default;

Session::Session(Session && src) = default;

Session & Session::operator=(Session && src) = default;

void Session::add_and_initialize_command(std::unique_ptr<Command> && command) {
    p_impl->add_and_initialize_command(std::move(command));
}

Command * Session::get_root_command() {
    return p_impl->get_root_command();
}

void Session::set_root_command(Command & command) {
    p_impl->set_root_command(command);
}

Command * Session::get_selected_command() {
    return p_impl->get_selected_command();
}

void Session::set_selected_command(Command * command) {
    p_impl->set_selected_command(command);
}

libdnf5::cli::ArgumentParser & Session::get_argument_parser() {
    return p_impl->get_argument_parser();
}

void Session::clear() {
    p_impl->clear();
}


Command::Command(Session & session, const std::string & name) : session{session} {
    // create a new argument parser command (owned by arg_parser)
    argument_parser_command = session.get_argument_parser().add_new_command(name);
    // set a backlink from the argument parser command to this command
    argument_parser_command->set_user_data(this);
}

Command::~Command() = default;

void Command::set_parent_command() {}

void Command::set_argument_parser() {}

void Command::register_subcommands() {}

void Command::pre_configure() {}

void Command::configure() {}

void Command::load_additional_packages() {}

void Command::run() {}

void Command::goal_resolved() {}

void Command::throw_missing_command() const {
    throw ArgumentParserMissingCommandError(M_("Missing command"), get_argument_parser_command()->get_id());
}
Session & Command::get_session() const noexcept {
    return session;
}

Command * Command::get_parent_command() const noexcept {
    auto * parser_parent = argument_parser_command->get_parent();
    if (!parser_parent)
        return nullptr;
    return static_cast<Command *>(parser_parent->get_user_data());
}

libdnf5::cli::ArgumentParser::Command * Command::get_argument_parser_command() const noexcept {
    return argument_parser_command;
}

void Command::register_subcommand(std::unique_ptr<Command> subcommand, libdnf5::cli::ArgumentParser::Group * group) {
    auto * sub_arg_parser_command = subcommand->get_argument_parser_command();
    get_argument_parser_command()->register_command(sub_arg_parser_command);

    if (group) {
        group->register_argument(sub_arg_parser_command);
    }

    session.add_and_initialize_command(std::move(subcommand));
}


Option::Option() = default;

Option::~Option() = default;


BoolOption::BoolOption(
    libdnf5::cli::session::Command & command,
    const std::string & long_name,
    char short_name,
    const std::string & desc,
    bool default_value,
    libdnf5::OptionBool * linked_option) {
    auto & parser = command.get_session().get_argument_parser();
    arg = parser.add_new_named_arg(long_name);

    if (!long_name.empty()) {
        arg->set_long_name(long_name);
    }

    if (short_name) {
        arg->set_short_name(short_name);
    }

    arg->set_description(desc);
    arg->set_const_value(default_value ? "false" : "true");
    if (linked_option) {
        conf = linked_option;
    } else {
        conf = dynamic_cast<libdnf5::OptionBool *>(
            parser.add_init_value(std::make_unique<libdnf5::OptionBool>(default_value)));
    }
    arg->link_value(conf);

    command.get_argument_parser_command()->register_named_arg(arg);
}

BoolOption::~BoolOption() = default;

bool BoolOption::get_value() const {
    return conf->get_value();
}

void BoolOption::set(libdnf5::Option::Priority priority, bool value) {
    return conf->set(priority, value);
}

libdnf5::cli::ArgumentParser::NamedArg * BoolOption::get_arg() {
    return arg;
}


AppendStringListOption::AppendStringListOption(
    libdnf5::cli::session::Command & command,
    const std::string & long_name,
    char short_name,
    const std::string & desc,
    const std::string & help,
    const std::string & allowed_values_regex,
    const bool icase,
    const std::string & delimiters) {
    auto & parser = command.get_session().get_argument_parser();
    conf = dynamic_cast<libdnf5::OptionStringList *>(parser.add_init_value(std::make_unique<libdnf5::OptionStringList>(
        std::vector<std::string>(), allowed_values_regex, icase, delimiters)));
    arg = parser.add_new_named_arg(long_name);

    if (!long_name.empty()) {
        arg->set_long_name(long_name);
    }

    if (short_name) {
        arg->set_short_name(short_name);
    }

    arg->set_description(desc);
    arg->set_arg_value_help(help);
    arg->set_has_value(true);
    arg->set_parse_hook_func(
        [this](
            [[maybe_unused]] ArgumentParser::NamedArg * arg, [[maybe_unused]] const char * option, const char * value) {
            conf->add(libdnf5::Option::Priority::COMMANDLINE, value);
            return true;
        });

    command.get_argument_parser_command()->register_named_arg(arg);
}

AppendStringListOption::AppendStringListOption(
    libdnf5::cli::session::Command & command,
    const std::string & long_name,
    char short_name,
    const std::string & desc,
    const std::string & help)
    : AppendStringListOption(command, long_name, short_name, desc, help, "", false) {}

AppendStringListOption::~AppendStringListOption() = default;

std::vector<std::string> AppendStringListOption::get_value() const {
    return conf->get_value();
}

libdnf5::cli::ArgumentParser::NamedArg * AppendStringListOption::get_arg() {
    return arg;
}

StringArgumentList::StringArgumentList(
    libdnf5::cli::session::Command & command, const std::string & name, const std::string & desc, int nargs) {
    auto & parser = command.get_session().get_argument_parser();

    conf = parser.add_new_values();
    arg = parser.add_new_positional_arg(
        name, nargs, parser.add_init_value(std::make_unique<libdnf5::OptionString>(nullptr)), conf);
    arg->set_description(desc);

    command.get_argument_parser_command()->register_positional_arg(arg);
}

StringArgumentList::StringArgumentList(
    libdnf5::cli::session::Command & command, const std::string & name, const std::string & desc)
    : StringArgumentList(command, name, desc, ArgumentParser::PositionalArg::UNLIMITED) {};

StringArgumentList::~StringArgumentList() = default;

std::vector<std::string> StringArgumentList::get_value() const {
    std::vector<std::string> result;

    for (auto & opt : *conf) {
        auto string_opt = dynamic_cast<libdnf5::OptionString *>(opt.get());
        result.emplace_back(string_opt->get_value());
    }

    return result;
}

libdnf5::cli::ArgumentParser::PositionalArg * StringArgumentList::get_arg() {
    return arg;
}


}  // namespace libdnf5::cli::session