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
|
// 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/>.
#ifndef LIBDNF5_CLI_SESSION_HPP
#define LIBDNF5_CLI_SESSION_HPP
#include "argument_parser.hpp"
#include "libdnf5-cli/defs.h"
#include <libdnf5/common/impl_ptr.hpp>
#include <libdnf5/conf/option_bool.hpp>
#include <libdnf5/conf/option_string.hpp>
#include <libdnf5/conf/option_string_list.hpp>
namespace libdnf5::cli::session {
class Command;
class LIBDNF_CLI_API Session {
public:
explicit Session();
~Session();
Session(const Session & src) = delete;
Session(Session && src);
Session & operator=(const Session & src) = delete;
Session & operator=(Session && src);
/// Store the command to the session and initialize it.
/// @since 5.0
void add_and_initialize_command(std::unique_ptr<Command> && command);
/// @return Root command that represents the main program.
/// The returned pointer must **not** be freed manually.
/// @since 5.0
Command * get_root_command();
/// Set `command` as the root command that represents the main program.
/// @since 5.0
void set_root_command(Command & command);
/// @return Selected (sub)command that a user specified on the command line.
/// The returned pointer must **not** be freed manually.
/// @since 5.0
Command * get_selected_command();
/// Set `command` as the selected (sub)command that a user specified on the command line.
/// We're only pointing to a command that is owned by the Session already.
/// @since 5.0
void set_selected_command(Command * command);
/// @return The underlying argument parser.
/// @since 5.0
libdnf5::cli::ArgumentParser & get_argument_parser();
/// Remove all commands from the session and argument parser.
/// @since 5.0
void clear();
private:
class LIBDNF_CLI_LOCAL Impl;
ImplPtr<Impl> p_impl;
};
class LIBDNF_CLI_API Command : public libdnf5::cli::ArgumentParserUserData {
public:
explicit Command(Session & session, const std::string & name);
virtual ~Command();
Command() = delete;
Command(const Command & src) = delete;
Command(Command && src) = delete;
Command & operator=(const Command & src) = delete;
Command & operator=(Command && src) = delete;
/// Sets a parent command and group. Can add a new group to the parent command.
/// @since 5.0
virtual void set_parent_command();
/// Set command arguments.
/// @since 5.0
virtual void set_argument_parser();
/// Register subcommands.
/// @since 5.0
virtual void register_subcommands();
/// Adjust configuration.
/// Called after parsing the command line and but before loading configuration files.
/// @since 5.0
virtual void pre_configure();
/// Adjust configuration.
/// Called after parsing the command line and loading configuration files.
/// @since 5.0
virtual void configure();
/// Loads additional packages that are not in the repositories.
/// @since 5.0
virtual void load_additional_packages();
/// Run the command.
/// @since 5.0
virtual void run();
/// Called immediately after the goal is resolved.
/// @since 5.0
virtual void goal_resolved();
/// Throw a ArgumentParserMissingCommandError exception with the command name in it
void throw_missing_command() const;
/// @return Pointer to the Session.
/// The returned pointer must **not** be freed manually.
/// @since 5.0
Session & get_session() const noexcept;
/// @return Pointer to the parent Command. Root command returns null because it has no parent.
/// The returned pointer must **not** be freed manually.
/// @since 5.0
Command * get_parent_command() const noexcept;
/// @return Pointer to the underlying argument parser command.
/// The returned pointer must **not** be freed manually.
/// @since 5.0
libdnf5::cli::ArgumentParser::Command * get_argument_parser_command() const noexcept;
protected:
/// Register a `subcommand` to the current command.
/// The command becomes owner of the `subcommand`.
/// @since 5.0
void register_subcommand(
std::unique_ptr<Command> subcommand, libdnf5::cli::ArgumentParser::Group * group = nullptr);
private:
Session & session;
libdnf5::cli::ArgumentParser::Command * argument_parser_command;
};
class LIBDNF_CLI_API Option {
public:
Option(const Option & src) = delete;
Option(Option && src) = delete;
~Option();
Option & operator=(const Option & src) = delete;
Option & operator=(Option && src) = delete;
protected:
Option();
};
class LIBDNF_CLI_API BoolOption : public Option {
public:
explicit 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 = nullptr);
~BoolOption();
/// @return Parsed value.
/// @since 5.0
bool get_value() const;
/// Set bool value with priority for the option
/// @param priority Priority
/// @param value Value
/// @since 5.0
void set(libdnf5::Option::Priority priority, bool value);
libdnf5::cli::ArgumentParser::NamedArg * get_arg();
protected:
libdnf5::OptionBool * conf{nullptr};
libdnf5::cli::ArgumentParser::NamedArg * arg{nullptr};
};
/// AppendStringListOption is a wrapper around NamedArg and OptionStringList
/// which allows specifying the argument multiple times and merging their values.
/// E.g. --whatrequires=tree --whatrequires=plant -> option contains: "tree, plant"
class LIBDNF_CLI_API AppendStringListOption : public Option {
public:
explicit AppendStringListOption(
libdnf5::cli::session::Command & command,
const std::string & long_name,
char short_name,
const std::string & desc,
const std::string & help);
explicit 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 = libdnf5::OptionStringList::get_default_delimiters());
~AppendStringListOption();
/// @return Parsed value.
/// @since 5.0
std::vector<std::string> get_value() const;
libdnf5::cli::ArgumentParser::NamedArg * get_arg();
protected:
libdnf5::OptionStringList * conf{nullptr};
libdnf5::cli::ArgumentParser::NamedArg * arg{nullptr};
};
class LIBDNF_CLI_API StringArgumentList : public Option {
public:
explicit StringArgumentList(
libdnf5::cli::session::Command & command, const std::string & name, const std::string & desc, int nargs);
explicit StringArgumentList(
libdnf5::cli::session::Command & command, const std::string & name, const std::string & desc);
~StringArgumentList();
/// @return Parsed value.
/// @since 5.0
std::vector<std::string> get_value() const;
libdnf5::cli::ArgumentParser::PositionalArg * get_arg();
protected:
std::vector<std::unique_ptr<libdnf5::Option>> * conf{nullptr};
libdnf5::cli::ArgumentParser::PositionalArg * arg{nullptr};
};
} // namespace libdnf5::cli::session
#endif // LIBDNF5_CLI_SESSION_HPP
|