paludis Version 3.0.2
The one true package mangler
example_package_id.cc

This example demonstrates how to use PackageID.

See example_action.cc for more on actions. See example_metadata_key.cc for more on metadata keys. See example_mask.cc for more on masks.

/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/** \file
*
* Example \ref example_package_id.cc "example_package_id.cc" .
*
* \ingroup g_package_id
*/
/** \example example_package_id.cc
*
* This example demonstrates how to use PackageID.
*
* See \ref example_action.cc "example_action.cc" for more on actions. See \ref
* example_metadata_key.cc "example_metadata_key.cc" for more on metadata keys.
* See \ref example_mask.cc "example_mask.cc" for more on masks.
*/
#include <paludis/paludis.hh>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <set>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
using std::left;
using std::setw;
using std::hex;
int main(int argc, char * argv[])
{
int exit_status(0);
try
{
CommandLine::get_instance()->run(argc, argv,
"example_package_id", "EXAMPLE_PACKAGE_ID_OPTIONS", "EXAMPLE_PACKAGE_ID_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()));
/* Fetch package IDs for 'sys-apps/paludis'. */
std::shared_ptr<const PackageIDSequence> ids((*env)[selection::AllVersionsSorted(
generator::Package(QualifiedPackageName("sys-apps/paludis")))]);
/* For each ID: */
for (const auto & id : *ids)
{
/* IDs can be written to an ostream. */
cout << *id << ":" << endl;
/* Start by outputting some basic properties: */
cout << left << setw(30) << " Name:" << " " << id->name() << endl;
cout << left << setw(30) << " Version:" << " " << id->version() << endl;
cout << left << setw(30) << " Repository Name:" << " " << id->repository_name() << endl;
/* The PackageID::canonical_form method should be used when
* outputting a package (the ostream << operator uses this). */
cout << left << setw(30) << " idcf_full:" << " " << id->canonical_form(idcf_full) << endl;
cout << left << setw(30) << " idcf_version:" << " " << id->canonical_form(idcf_version) << endl;
cout << left << setw(30) << " idcf_no_version:" << " " << id->canonical_form(idcf_no_version) << endl;
cout << left << setw(30) << " idcf_no_name:" << " " << id->canonical_form(idcf_no_name) << endl;
/* Let's see what keys we have. Other examples cover keys in depth,
* so we'll just use the basic methods here. */
cout << left << setw(30) << " Keys:" << " " << endl;
for (const auto & key : id->metadata())
cout << left << setw(30) << (" " + key->raw_name() + ":") << " " << key->human_name() << endl;
/* And what about masks? Again, these are covered in depth
* elsewhere. */
if (id->masked())
{
cout << left << setw(30) << " Masks:" << " " << endl;
for (const auto & mask : id->masks())
cout << left << setw(30) << (" " + stringify(mask->key()) + ":") << " " << mask->description() << endl;
}
/* Let's see which actions we support. There's no particularly nice
* way of doing this, since it's not something we'd expect to be
* doing. */
std::set<std::string> actions;
{
if (id->supports_action(install_action))
actions.insert("install");
if (id->supports_action(uninstall_action))
actions.insert("uninstall");
if (id->supports_action(pretend_action))
actions.insert("pretend");
if (id->supports_action(config_action))
actions.insert("config");
if (id->supports_action(fetch_action))
actions.insert("fetch");
if (id->supports_action(info_action))
actions.insert("info");
}
cout << left << setw(30) << " Actions:" << " " << join(actions.begin(), actions.end(), " ") << endl;
/* And various misc methods. Clients don't usually use these
* directly. */
cout << left << setw(30) << " extra_hash_value:" << " " << "0x" << hex << id->extra_hash_value() << endl;
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_status;
}
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 name.hh:112
static CommandLine * get_instance() PALUDIS_ATTRIBUTE((warn_unused_result))
Definition action.hh:657
Definition generator.hh:179
Definition selection.hh:172
T_ join(I_ i, I_ end, const T_ &joiner)
Definition join.hh:45
std::string stringify(const T_ &item)
Definition stringify.hh:166
Definition about_metadata-fwd.hh:24