aboutsummaryrefslogtreecommitdiff
path: root/doc/api/cplusplus/examples/example_package_database.cc
blob: 521c7e4e18437cd5561136a6fde97f84bacf7e15 (plain)
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/** \file
 *
 * Example \ref example_package_database.cc "example_package_database.cc" .
 *
 * \ingroup g_package_database
 */

/** \example example_package_database.cc
 *
 * This example demonstrates how to use PackageDatabase.
 */

#include <paludis/paludis.hh>
#include "example_command_line.hh"
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <map>

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_flattener", "EXAMPLE_DEP_SPEC_FLATTENER_OPTIONS", "EXAMPLE_DEP_SPEC_FLATTENER_CMDLINE");

        /* We start with an Environment, respecting the user's '--environment' choice. */
        tr1::shared_ptr<Environment> env(EnvironmentMaker::get_instance()->make_from_spec(
                    CommandLine::get_instance()->a_environment.argument()));

        /* The most useful PackageDatabase method is PackageDatabase::query,
         * which is covered in other examples. But there are others: */
        if (env->package_database()->has_repository_named(RepositoryName("gentoo")))
        {
            tr1::shared_ptr<const Repository> repo(env->package_database()->fetch_repository(RepositoryName("gentoo")));
            cout << "Repository 'gentoo' exists, and has format '" <<
                (repo->format_key() ? repo->format_key()->value() : "") << "'" << endl;
        }

        /* Our favourite repository is the least important non-special (e.g.
         * virtual) installable repository. Some clients use this as a hint to
         * not display a ::repository suffix for things from this repository. */
        cout << "Our favourite repository is '" << env->package_database()->favourite_repository() << "'" << endl;

        /* Users often expect to be able to refer to a package by its name part
         * only (e.g. 'foo' rather than 'app-misc/foo'). This has to be
         * disambiguated as follows: */
        try
        {
            QualifiedPackageName name(env->package_database()->fetch_unique_qualified_package_name(
                        PackageNamePart("git")));
            cout << "The only package named 'git' is '" << name << "'" << endl;
        }
        catch (const NoSuchPackageError & e)
        {
            cout << "There is no package named 'git'" << endl;
        }
        catch (const AmbiguousPackageNameError & e)
        {
            cout << "There are several packages named 'git':" << endl;
            for (AmbiguousPackageNameError::OptionsConstIterator o(e.begin_options()), o_end(e.end_options()) ;
                    o != o_end ; ++o)
                cout << "    " << *o << 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;
}