dnf5: Move create(_installed)_from_repo_option to public API

Functions create_from_repo_option and create_installed_from_repo_option
were moved to public API (to "shared_options.[ch]pp" files).

So now they can be used in dnf5 plugins. The builddep plugin was
switched to use create_from_repo_option function.
This commit is contained in:
Jaroslav Rohel 2025-07-24 11:40:32 +02:00
parent 8b157308f3
commit ada83f6de4
16 changed files with 78 additions and 179 deletions

View File

@ -123,28 +123,7 @@ void BuildDepCommand::set_argument_parser() {
auto skip_unavailable = std::make_unique<SkipUnavailableOption>(*this);
create_allow_downgrade_options(*this);
{
auto from_repo_opt = parser.add_new_named_arg("from-repo");
from_repo_opt->set_long_name("from-repo");
from_repo_opt->set_description(
_("The following items can be selected only from the specified repositories. All enabled repositories will "
"still be used to satisfy dependencies."));
from_repo_opt->set_has_value(true);
from_repo_opt->set_arg_value_help(_("REPO_ID,..."));
from_repo_opt->set_parse_hook_func(
[this](libdnf5::cli::ArgumentParser::NamedArg *, [[maybe_unused]] const char * option, const char * value) {
if (from_repos.empty()) {
from_repos = libdnf5::OptionStringList(value).get_value();
} else {
if (from_repos != libdnf5::OptionStringList(value).get_value()) {
throw libdnf5::cli::ArgumentParserConflictingArgumentsError(
M_("\"--from_repo\" already defined with diferent value"));
}
}
return true;
});
cmd.register_named_arg(from_repo_opt);
}
create_from_repo_option(*this, from_repos, true);
create_store_option(*this);

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "distro-sync.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
#include <libdnf5/conf/option_string.hpp>
#include <libdnf5/utils/bgettext/bgettext-lib.h>

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "do.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
#include <libdnf5/conf/const.hpp>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "downgrade.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
namespace dnf5 {

View File

@ -19,7 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "download.hpp"
#include "../from_repo.hpp"
#include "dnf5/shared_options.hpp"
#include <libdnf5/conf/option_string.hpp>

View File

@ -1,89 +0,0 @@
/*
Copyright Contributors to the dnf5 project.
This file is part of dnf5: https://github.com/rpm-software-management/dnf5/
Dnf5 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.
Dnf5 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 dnf5. If not, see <https://www.gnu.org/licenses/>.
*/
#include "from_repo.hpp"
#include <libdnf5-cli/argument_parser.hpp>
#include <libdnf5/conf/option_string_list.hpp>
#include <libdnf5/utils/bgettext/bgettext-lib.h>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>
namespace dnf5 {
void create_from_repo_option(Command & command, std::vector<std::string> & from_repos, bool detect_conflict) {
auto & parser = command.get_context().get_argument_parser();
auto from_repo_opt = parser.add_new_named_arg("from-repo");
from_repo_opt->set_long_name("from-repo");
from_repo_opt->set_description(
_("The following items can be selected only from the specified repositories. All enabled repositories will "
"still be used to satisfy dependencies."));
from_repo_opt->set_has_value(true);
from_repo_opt->set_arg_value_help(_("REPO_ID,..."));
from_repo_opt->set_parse_hook_func(
[&command, &from_repos, detect_conflict](
libdnf5::cli::ArgumentParser::NamedArg *, [[maybe_unused]] const char * option, const char * value) {
if (!detect_conflict || from_repos.empty()) {
from_repos = libdnf5::OptionStringList(value).get_value();
// We need to ensure repositories for requested packages are enabled. We'll explicitly enable them
// using setopts as a safeguard. setopts will be applied later during repository configuration loading.
for (const auto & repoid_pattern : from_repos) {
command.get_context().get_setopts().emplace_back(repoid_pattern + ".enabled", "1");
}
} else {
if (from_repos != libdnf5::OptionStringList(value).get_value()) {
throw libdnf5::cli::ArgumentParserConflictingArgumentsError(
M_("\"--from_repo\" already defined with diferent value"));
}
}
return true;
});
command.get_argument_parser_command()->register_named_arg(from_repo_opt);
}
libdnf5::cli::ArgumentParser::NamedArg * create_installed_from_repo_option(
Command & command, std::vector<std::string> & from_repos, bool detect_conflict) {
auto & parser = command.get_context().get_argument_parser();
auto * from_repo_opt = parser.add_new_named_arg("installed-from-repo");
from_repo_opt->set_long_name("installed-from-repo");
from_repo_opt->set_description(
_("Filters installed packages by the ID of the repository they were installed from."));
from_repo_opt->set_has_value(true);
from_repo_opt->set_arg_value_help(_("REPO_ID,..."));
from_repo_opt->set_parse_hook_func(
[&from_repos, detect_conflict](
libdnf5::cli::ArgumentParser::NamedArg *, [[maybe_unused]] const char * option, const char * value) {
if (!detect_conflict || from_repos.empty()) {
from_repos = libdnf5::OptionStringList(value).get_value();
} else {
if (from_repos != libdnf5::OptionStringList(value).get_value()) {
throw libdnf5::cli::ArgumentParserConflictingArgumentsError(
M_("\"--installed-from_repo\" already defined with diferent value"));
}
}
return true;
});
command.get_argument_parser_command()->register_named_arg(from_repo_opt);
return from_repo_opt;
}
} // namespace dnf5

View File

@ -1,47 +0,0 @@
/*
Copyright Contributors to the dnf5 project.
This file is part of dnf5: https://github.com/rpm-software-management/dnf5/
Dnf5 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.
Dnf5 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 dnf5. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef DNF5_COMMANDS_FROM_REPO_HPP
#define DNF5_COMMANDS_FROM_REPO_HPP
#include <dnf5/context.hpp>
#include <string>
#include <vector>
namespace dnf5 {
// Creates a new named argument "--from-repo=REPO_ID,...".
// When the argument is used, the list of REPO_IDs is split and the items are stored in the `from_repos` vector.
// If `detect_conflict` is true, a `libdnf5::cli::ArgumentParserConflictingArgumentsError` exception is thrown
// if "--from-repo" was already defined with a different value.
void create_from_repo_option(Command & command, std::vector<std::string> & from_repos, bool detect_conflict);
// Creates a new named argument "--installed-from-repo=REPO_ID,...".
// When the argument is used, the list of REPO_IDs is split and the items are stored in the `from_repos` vector.
// If `detect_conflict` is true, a `libdnf5::cli::ArgumentParserConflictingArgumentsError` exception is thrown
// if "--from-repo" was already defined with a different value.
libdnf5::cli::ArgumentParser::NamedArg * create_installed_from_repo_option(
Command & command, std::vector<std::string> & from_repos, bool detect_conflict);
} // namespace dnf5
#endif // DNF5_COMMANDS_FROM_REPO_HPP

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "install.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
#include <libdnf5/conf/const.hpp>

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "list.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
#include <libdnf5-cli/output/package_list_sections.hpp>
#include <libdnf5/rpm/package_query.hpp>

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "reinstall.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
namespace dnf5 {

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "remove.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
namespace dnf5 {

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "repoquery.hpp"
#include "../from_repo.hpp"
#include "libdnf5-cli/output/adapters/package_tmpl.hpp"
#include <dnf5/shared_options.hpp>

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "swap.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
namespace fs = std::filesystem;

View File

@ -19,8 +19,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "upgrade.hpp"
#include "../from_repo.hpp"
#include <dnf5/shared_options.hpp>
#include <libdnf5/conf/const.hpp>

View File

@ -71,6 +71,23 @@ DNF_API void create_offline_option(dnf5::Command & command);
/// Create the `--json` option for a command provided as an argument.
DNF_API void create_json_option(dnf5::Command & command);
/// Creates a new named argument "--from-repo=REPO_ID,...".
/// When the argument is used, the list of REPO_IDs is split and the IDs are stored in the `repo_ids` vector.
/// If `detect_conflict` is true, a `libdnf5::cli::ArgumentParserConflictingArgumentsError` exception is thrown
/// if "--from-repo" was already used with a different value.
/// Newly created argument is registered to the `command` and returned.
DNF_API libdnf5::cli::ArgumentParser::NamedArg * create_from_repo_option(
Command & command, std::vector<std::string> & repo_ids, bool detect_conflict);
/// Creates a new named argument "--installed-from-repo=REPO_ID,...".
/// When the argument is used, the list of REPO_IDs is split and the IDs are stored in the `repo_ids` vector.
/// If `detect_conflict` is true, a `libdnf5::cli::ArgumentParserConflictingArgumentsError` exception is thrown
/// if "--installed-from-repo" was already used with a different value.
/// Newly created argument is registered to the `command` and returned.
DNF_API libdnf5::cli::ArgumentParser::NamedArg * create_installed_from_repo_option(
Command & command, std::vector<std::string> & repo_ids, bool detect_conflict);
} // namespace dnf5
#endif // DNF5_COMMANDS_SHARED_OPTIONS_HPP

View File

@ -157,4 +157,64 @@ void create_json_option(dnf5::Command & command) {
}
libdnf5::cli::ArgumentParser::NamedArg * create_from_repo_option(
Command & command, std::vector<std::string> & repo_ids, bool detect_conflict) {
auto & parser = command.get_context().get_argument_parser();
auto * const from_repo_opt = parser.add_new_named_arg("from-repo");
from_repo_opt->set_long_name("from-repo");
from_repo_opt->set_description(
_("The following items can be selected only from the specified repositories. All enabled repositories will "
"still be used to satisfy dependencies."));
from_repo_opt->set_has_value(true);
from_repo_opt->set_arg_value_help(_("REPO_ID,..."));
from_repo_opt->set_parse_hook_func(
[&command, &repo_ids, detect_conflict](
libdnf5::cli::ArgumentParser::NamedArg *, [[maybe_unused]] const char * option, const char * value) {
if (!detect_conflict || repo_ids.empty()) {
repo_ids = libdnf5::OptionStringList(value).get_value();
// We need to ensure repositories for requested packages are enabled. We'll explicitly enable them
// using setopts as a safeguard. setopts will be applied later during repository configuration loading.
for (const auto & repoid_pattern : repo_ids) {
command.get_context().get_setopts().emplace_back(repoid_pattern + ".enabled", "1");
}
} else {
if (repo_ids != libdnf5::OptionStringList(value).get_value()) {
throw libdnf5::cli::ArgumentParserConflictingArgumentsError(
M_("\"--from_repo\" already defined with diferent value"));
}
}
return true;
});
command.get_argument_parser_command()->register_named_arg(from_repo_opt);
return from_repo_opt;
}
libdnf5::cli::ArgumentParser::NamedArg * create_installed_from_repo_option(
Command & command, std::vector<std::string> & repo_ids, bool detect_conflict) {
auto & parser = command.get_context().get_argument_parser();
auto * const installed_from_repo_opt = parser.add_new_named_arg("installed-from-repo");
installed_from_repo_opt->set_long_name("installed-from-repo");
installed_from_repo_opt->set_description(
_("Filters installed packages by the ID of the repository they were installed from."));
installed_from_repo_opt->set_has_value(true);
installed_from_repo_opt->set_arg_value_help(_("REPO_ID,..."));
installed_from_repo_opt->set_parse_hook_func(
[&repo_ids, detect_conflict](
libdnf5::cli::ArgumentParser::NamedArg *, [[maybe_unused]] const char * option, const char * value) {
if (!detect_conflict || repo_ids.empty()) {
repo_ids = libdnf5::OptionStringList(value).get_value();
} else {
if (repo_ids != libdnf5::OptionStringList(value).get_value()) {
throw libdnf5::cli::ArgumentParserConflictingArgumentsError(
M_("\"--installed-from_repo\" already defined with diferent value"));
}
}
return true;
});
command.get_argument_parser_command()->register_named_arg(installed_from_repo_opt);
return installed_from_repo_opt;
}
} // namespace dnf5