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

/** \file
 *
 * Example \ref example_version_spec.cc "example_version_spec.cc" .
 *
 * \ingroup g_names
 */

/** \example example_version_spec.cc
 *
 * This example demonstrates how to use VersionSpec.
 */

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

using namespace paludis;
using namespace examples;

using std::cout;
using std::endl;
using std::setw;
using std::left;
using std::boolalpha;
using std::hex;

int main(int argc, char * argv[])
{
    try
    {
        CommandLine::get_instance()->run(argc, argv,
                "example_version_spec", "EXAMPLE_VERSION_SPEC_OPTIONS", "EXAMPLE_VERSION_SPEC_CMDLINE");

        /* Make a set of versions. Use user_version_spec_options() for the
         * options parameter for VersionSpec's constructor for handling any
         * user-inputted data. */
        std::set<VersionSpec> versions;
        versions.insert(VersionSpec("1.0", user_version_spec_options()));
        versions.insert(VersionSpec("1.1", user_version_spec_options()));
        versions.insert(VersionSpec("1.2", user_version_spec_options()));
        versions.insert(VersionSpec("1.2-r1", user_version_spec_options()));
        versions.insert(VersionSpec("2.0", user_version_spec_options()));
        versions.insert(VersionSpec("2.0-try1", user_version_spec_options()));
        versions.insert(VersionSpec("2.0-scm", user_version_spec_options()));
        versions.insert(VersionSpec("9999", user_version_spec_options()));

        /* For each version... */
        for (std::set<VersionSpec>::const_iterator v(versions.begin()), v_end(versions.end()) ;
                v != v_end ; ++v)
        {
            /* Versions are stringifiable */
            cout << *v << ":" << endl;

            /* Show the output of various members. Not all of these are of much
             * direct use. */
            cout << "    " << left << setw(24) << "Hash value:" << " " << "0x" << hex << v->hash() << endl;
            cout << "    " << left << setw(24) << "Remove revision:" << " " << v->remove_revision() << endl;
            cout << "    " << left << setw(24) << "Revision only:" << " " << v->revision_only() << endl;
            cout << "    " << left << setw(24) << "Bump:" << " " << v->bump() << endl;
            cout << "    " << left << setw(24) << "Is scm?" << " " << boolalpha << v->is_scm() << endl;
            cout << "    " << left << setw(24) << "Has -try?" << " " << boolalpha << v->has_try_part() << endl;
            cout << "    " << left << setw(24) << "Has -scm?" << " " << boolalpha << v->has_scm_part() << 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_SUCCESS;
}