File: Options.h

package info (click to toggle)
pentobi 29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,892 kB
  • sloc: cpp: 25,719; javascript: 875; xml: 40; makefile: 13; sh: 6
file content (112 lines) | stat: -rw-r--r-- 3,560 bytes parent folder | download
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
107
108
109
110
111
112
//-----------------------------------------------------------------------------
/** @file libboardgame_base/Options.h
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#ifndef LIBBOARDGAME_BASE_OPTIONS_H
#define LIBBOARDGAME_BASE_OPTIONS_H

#include <map>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
#include "StringUtil.h"

namespace libboardgame_base {

using namespace std;

//-----------------------------------------------------------------------------

class OptionError
    : public runtime_error
{
    using runtime_error::runtime_error;
};

//-----------------------------------------------------------------------------

/** Parser for command line options.
    The syntax of options is similar to GNU getopt. Options start with "--"
    and an option name. Options have optional short (single-character) names
    that are used with a single "-" and can be combined if all but the last
    option have no value. A single "--" stops option parsing to support
    non-option arguments that start with "-". */
class Options
{
public:
    /** Create options from arguments to main().
        @param argc
        @param argv
        @param specs A string per option that describes the option. The
        description is the long name of the option, followed by and optional
        '|' and a character for the short name of the option, followed  by an
        optional ':' if the option needs a value.
        @throws OptionError on error */
    Options(int argc, const char** argv, const vector<string>& specs);

    /** Overloaded version for con-const character strings in argv.
        Needed because the portable signature of main is (int, char**).
        argv is not modified by this constructor.  */
    Options(int argc, char** argv, const vector<string>& specs);

    /** Check if an option exists in the command line arguments.
        @param name The (long) option name.  */
    bool contains(const string& name) const;

    string get(const string& name) const;

    string get(const string& name, const string& default_value) const;

    /** Get option value.
        @param name The (long) option name.
        @throws OptionError If option does not exist or has the wrong type. */
    template<typename T>
    T get(const string& name) const;

    /** Get option value or default value.
        @param name The (long) option name.
        @param default_value A default value.
        @return The option value or the default value if the option does not
        exist. */
    template<typename T>
    T get(const string& name, const T& default_value) const;

    /** Remaining command line arguments that are not an option or an option
        value. */
    const vector<string>& get_args() const { return m_args; }

private:
    set<string> m_names;

    vector<string> m_args;

    map<string, string> m_map;

    void check_name(const string& name) const;
};

template<typename T>
T Options::get(const string& name) const
{
    T t;
    if (! from_string(get(name), t))
        throw OptionError("Option --" + name + " has invalid type");
    return t;
}

template<typename T>
T Options::get(const string& name, const T& default_value) const
{
    if (! contains(name))
        return default_value;
    return get<T>(name);
}

//-----------------------------------------------------------------------------

} // namespace libboardgame_base

#endif // LIBBOARDGAME_BASE_OPTIONS_H