Skip to content

Commit

Permalink
context: Prepare shared json option
Browse files Browse the repository at this point in the history
For requesting machine-readable output in JSON format.
  • Loading branch information
jan-kolarik authored and kontura committed Jun 5, 2024
1 parent 78101dc commit ec16aca
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
12 changes: 12 additions & 0 deletions dnf5/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ class Context::Impl {
void set_should_store_offline(bool should_store_offline) { this->should_store_offline = should_store_offline; }
bool get_should_store_offline() const { return should_store_offline; }

void set_json_output_requested(bool json_output) { this->json_output = json_output; }
bool get_json_output_requested() const { return json_output; }

libdnf5::Base & get_base() { return base; };

std::vector<std::pair<std::string, std::string>> & get_setopts() { return setopts; }
Expand Down Expand Up @@ -206,6 +209,7 @@ class Context::Impl {
const char * comment{nullptr};

bool should_store_offline = false;
bool json_output = false;

bool quiet{false};
bool dump_main_config{false};
Expand Down Expand Up @@ -658,6 +662,14 @@ bool Context::get_should_store_offline() const {
return p_impl->get_should_store_offline();
}

void Context::set_json_output_requested(bool json_output) {
p_impl->set_json_output_requested(json_output);
}

bool Context::get_json_output_requested() const {
return p_impl->get_json_output_requested();
}

libdnf5::Base & Context::get_base() {
return p_impl->get_base();
};
Expand Down
3 changes: 3 additions & 0 deletions dnf5/include/dnf5/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class Context : public libdnf5::cli::session::Session {
void set_should_store_offline(bool should_store_offline);
bool get_should_store_offline() const;

void set_json_output_requested(bool json_output);
bool get_json_output_requested() const;

libdnf5::Base & get_base();

std::vector<std::pair<std::string, std::string>> & get_setopts();
Expand Down
4 changes: 3 additions & 1 deletion dnf5/include/dnf5/shared_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ void create_downloadonly_option(dnf5::Command & command);
/// The value is stored in Context::transaction_store_path.
void create_store_option(dnf5::Command & command);


/// Create the `--offline` option for a command provided as an argument.
void create_offline_option(dnf5::Command & command);

/// Create the `--json` option for a command provided as an argument.
void create_json_option(dnf5::Command & command);

} // namespace dnf5

#endif // DNF5_COMMANDS_SHARED_OPTIONS_HPP
17 changes: 17 additions & 0 deletions dnf5/shared_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,22 @@ void create_store_option(dnf5::Command & command) {
});
}

void create_json_option(dnf5::Command & command) {
auto & ctx = command.get_context();
auto & parser = command.get_context().get_argument_parser();
auto json = parser.add_new_named_arg("json");
json->set_long_name("json");
json->set_description("Request json output format");
json->set_const_value("true");
json->set_parse_hook_func([&ctx](
[[maybe_unused]] libdnf5::cli::ArgumentParser::NamedArg * arg,
[[maybe_unused]] const char * option,
[[maybe_unused]] const char * value) {
ctx.set_json_output_requested(true);
return true;
});
command.get_argument_parser_command()->register_named_arg(json);
}


} // namespace dnf5

0 comments on commit ec16aca

Please sign in to comment.