paludis Version 3.0.2
The one true package mangler
example_dep_spec.cc

This example demonstrates how to handle dependency specs.

See example_dep_label.cc for labels.

/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/** \file
*
* Example \ref example_dep_spec.cc "example_dep_spec.cc" .
*
* \ingroup g_dep_spec
*/
/** \example example_dep_spec.cc
*
* This example demonstrates how to handle dependency specs.
*
* See \ref example_dep_label.cc "example_dep_label.cc" for labels.
*/
#include <paludis/paludis.hh>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <map>
#include <sstream>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
using std::setw;
using std::left;
int main(int argc, char * argv[])
{
try
{
CommandLine::get_instance()->run(argc, argv,
"example_dep_spec", "EXAMPLE_DEP_SPEC_OPTIONS", "EXAMPLE_DEP_SPEC_CMDLINE");
/* We start with an Environment, respecting the user's '--environment' choice. */
std::shared_ptr<Environment> env(EnvironmentFactory::get_instance()->create(
CommandLine::get_instance()->a_environment.argument()));
/* For each command line parameter... */
for (const auto & param : CommandLine::get_instance()->parameters())
{
/* Create a PackageDepSpec from the parameter. For user-inputted
* data, parse_user_package_dep_spec() should be used. If wildcards
* are to be permitted, the updso_allow_wildcards option should be
* included. If you might be getting a set, also include
* updso_throw_if_set and catch the GotASetNotAPackageDepSpec
* exception. If data about the spec is known at compile time,
* make_package_dep_spec() should be used instead. */
parse_user_package_dep_spec(param, env.get(), {updso_allow_wildcards}));
/* Display information about the PackageDepSpec. */
cout << "Information about '" << spec << "':" << endl;
if (spec.package_ptr())
cout << " " << left << setw(24) << "Package:" << " " << *spec.package_ptr() << endl;
cout << " " << left << setw(24) << "Category part:" << " " << *spec.category_name_part_ptr() << endl;
cout << " " << left << setw(24) << "Package part:" << " " << *spec.package_name_part_ptr() << endl;
if (spec.version_requirements_ptr() && ! spec.version_requirements_ptr()->empty())
{
cout << " " << left << setw(24) << "Version requirements:" << " ";
bool need_join(false);
for (const auto & requirement : *spec.version_requirements_ptr())
{
if (need_join)
{
switch (spec.version_requirements_mode())
{
case vr_and:
cout << " and ";
break;
case vr_or:
cout << " or ";
break;
case last_vr:
throw InternalError(PALUDIS_HERE, "Bad version_requirements_mode");
}
}
cout << requirement.version_operator() << requirement.version_spec();
need_join = true;
}
cout << endl;
}
cout << " " << left << setw(24) << "Slot:" << " " << *spec.slot_requirement_ptr() << endl;
if (spec.in_repository_ptr())
cout << " " << left << setw(24) << "In repository:" << " " <<
*spec.in_repository_ptr() << endl;
if (spec.from_repository_ptr())
cout << " " << left << setw(24) << "From repository:" << " " <<
*spec.from_repository_ptr() << endl;
cout << " " << left << setw(24) << "Installed at path:" << " " <<
*spec.installed_at_path_ptr() << endl;
cout << " " << left << setw(24) << "Installable to path:" << " " <<
spec.installable_to_path_ptr()->path() << ", " <<
spec.installable_to_path_ptr()->include_masked() << endl;
cout << " " << left << setw(24) << "Installable to repository:" << " " <<
spec.installable_to_repository_ptr()->repository() << ", " <<
spec.installable_to_repository_ptr()->include_masked() << endl;
{
cout << " " << left << setw(24) << "Additional requirements:" << " ";
bool need_join(false);
for (const auto & requirement : *spec.additional_requirements_ptr())
{
if (need_join)
cout << " and ";
cout << requirement->as_raw_string() + " (meaning: " + requirement->as_human_string(nullptr) + ")";
need_join = true;
}
cout << endl;
}
/* And display packages matching that spec */
cout << " " << left << setw(24) << "Matches:" << " ";
std::shared_ptr<const PackageIDSequence> ids((*env)[selection::AllVersionsSorted(
generator::Matches(spec, nullptr, { }))]);
bool need_indent(false);
for (const auto & id : *ids)
{
if (need_indent)
cout << " " << left << setw(24) << "" << " ";
cout << *id << endl;
need_indent = true;
}
cout << endl;
}
}
catch (const Exception & e)
{
/* Paludis exceptions can provide a handy human-readable backtrace and
* an explanation message. Where possible, these should be displayed. */
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.backtrace("\n * ")
<< e.message() << " (" << e.what() << ")" << endl;
return EXIT_FAILURE;
}
catch (const std::exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.what() << endl;
return EXIT_FAILURE;
}
catch (...)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * Unknown exception type. Ouch..." << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Definition exception.hh:77
std::string backtrace(const std::string &delim) const
const char * what() const noexcept override
const std::string & message() const noexcept
Definition exception.hh:134
Definition dep_spec.hh:348
std::shared_ptr< const AdditionalPackageDepSpecRequirements > additional_requirements_ptr() const
std::shared_ptr< const FSPath > installed_at_path_ptr() const
std::shared_ptr< const SlotRequirement > slot_requirement_ptr() const
std::shared_ptr< const InstallableToRepository > installable_to_repository_ptr() const
std::shared_ptr< const InstallableToPath > installable_to_path_ptr() const
std::shared_ptr< const VersionRequirements > version_requirements_ptr() const
std::shared_ptr< const QualifiedPackageName > package_ptr() const
VersionRequirementsMode version_requirements_mode() const
std::shared_ptr< const CategoryNamePart > category_name_part_ptr() const
std::shared_ptr< const PackageNamePart > package_name_part_ptr() const
std::shared_ptr< const RepositoryName > in_repository_ptr() const
std::shared_ptr< const RepositoryName > from_repository_ptr() const
static CommandLine * get_instance() PALUDIS_ATTRIBUTE((warn_unused_result))
Definition generator.hh:160
Definition selection.hh:172
PackageDepSpec parse_user_package_dep_spec(const std::string &, const Environment *const, const UserPackageDepSpecOptions &, const Filter &=filter::All()) PALUDIS_VISIBLE
@ vr_and
< Must match one
Definition version_requirements-fwd.hh:57
@ last_vr
< Must match all
Definition version_requirements-fwd.hh:58
#define PALUDIS_HERE
Definition exception.hh:217
Definition about_metadata-fwd.hh:24