summaryrefslogtreecommitdiffstats
path: root/task1/getoptwrapper.h
blob: 21270f7d8c91e70a9a58a18ba33b49916507ed3b (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
 * $Id: getoptwrapper.h 2 2009-10-31 02:48:23Z l0728348 $
 *
 * Copyright 2009
 *
 * @author Manuel Mausz (0728348)
 * @brief  Wraps around getopt_long() using two classes
 *         class CommandOption represents a valid commandline option
 *         class CommandOptionParse calls getopt_long
 *         and generates usage message
 */

#ifndef GETOPTWRAPPER_H
#define GETOPTWRAPPER_H

#include <string>
#include <list>
#include <vector>
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#include <getopt.h>

/* forward declarations */
class CommandOption;
class CommandOptionParse;

/** default list containing commandline options */
extern std::list<CommandOption *> cmdoptionlist;

/**
 * every instance of CommandOption represents a valid commandline option
 * use CommandOptionParse to parse them
 */
class CommandOption
{
  /* be friend with CommandOptionParse so it can use our privates */
  friend class CommandOptionParse;

  public:
    /** commandline option argument */
    enum Type
    {
      noArg   = 0, /* no argument */
      needArg = 1, /* argument required */
      optArg  = 2, /* argument optional */
    };

    /**
     * @brief  Default ctor. Adds commandline option to commandline list
     * @param  longopt long commandline option. empty string to omit
     * @param  shortopt short commandline option. 0 to omit
     * @param  description description. will be printed in usage
     * @param  type type of argument
     * @param  cmdlist list to add this commandline option
     */
    CommandOption(const std::string& longopt, const char shortopt, const std::string& description,
        const Type& type, std::list<CommandOption *>& cmdlist = cmdoptionlist)
      : m_longopt(longopt), m_shortopt(shortopt), m_description(description),
        m_type(type), m_count(0), m_value("")
    {
      if (!m_longopt.empty() || m_shortopt != 0)
        cmdlist.push_back(this);
    }

    /**
     * @brief  Default dtor
     */
    virtual ~CommandOption()
    {}

    /**
     * @brief  generate a usage string/valid syntax
     *         for this commandline option
     * @return usage string for this commandline option
     */
    const std::string printParameters();

    /**
     * @brief  get number of commandline matches found
     * @return number of commandline matches found
     */
    unsigned count()
    {
      return m_count;
    }

    /**
     * @brief  get number of commandline matches found
     * @return number of commandline matches found
     */
    operator unsigned()
    {
      return count();
    }

    /**
     * @brief  returns first valid commandline option argument
     *         if the commandline option is found a second time,
     *         the argument is omitted
     * @return first valid argument for this commandline option
     */
    const std::string value()
    {
      return m_value;
    }

    /**
     * @brief  returns first valid commandline option argument
     *         if the commandline option is found a second time,
     *         the argument is omitted
     * @return first valid argument for this commandline option
     */
    operator std::string()
    {
      return value();
    }

  private:
    /**
     * @brief  gets called by CommandOptionParse if the commandline option
     *         is found. Parses and stores the argument. Only the first call
     *         will be stored.
     * @param value commandline option argument
     */
    void foundOption(const char *value);

    std::string m_longopt;
    char m_shortopt;
    std::string m_description;
    Type m_type;
    unsigned m_count;
    std::string m_value;
};

/*----------------------------------------------------------------------------*/

/**
 * parses argc/argv using getopt_long and a list of instances of CommandOption
 * also generates usage message
 */
class CommandOptionParse
{
  public:
    /** list of pointers of CommandOption */
    typedef std::list<CommandOption *> CommandOptionList;

    /**
     * @brief  Default ctor.
     * @param  argc argc of main
     * @param  argv argv of main
     * @param  description will be printed in usage before CommandOption list
     * @param  cmdlist list to add this commandline option
     */
    CommandOptionParse(int argc, char* argv[], const std::string& description,
        std::list<CommandOption *>& cmdlist = cmdoptionlist)
      : m_argc(argc), m_argv(argv), m_description(description), m_cmdlist(cmdlist),
        m_longopts(NULL)
    {}

    /**
     * @brief  Default dtor.
     */
    virtual ~CommandOptionParse()
    {
      if (m_longopts != NULL)
        delete[] m_longopts;
    }

    /**
     * @brief  adds valid synopsis to usage message
     * @param  synopsis synopsis
     */
    void addSynopsis(const std::string& synopsis)
    {
      m_synopsis.push_back(synopsis);
    }

    /**
     * @brief  starts parsing argc/argv
     * @return true on success. false otherwise
     */
    bool parse();

    /**
     * @brief  generates usage message
     * @return usage message as string
     */
    const std::string usage();

    /**
     * @brief  generates usage message and prints them on ostream
     * @return reference to ostream
     */
    friend std::ostream& operator<<(std::ostream& os, CommandOptionParse& cmdoptions)
    {
      os << cmdoptions.usage();
      return os;
    }

    /**
     * @brief  directly access a valid/parsed commandline option
     *         using its longopt parameter
     * @param  longopt parameter "longopt" of commandline option
     * @return reference to commandline option
     * @throw  std::out_of_range if the commandline option cannot be found
     */
    CommandOption& operator[](const std::string& longopt);

    /**
     * @brief  directly access a valid/parsed commandline option
     *         using its shortopt parameter
     * @param  shortopt parameter "shortopt" of commandline option
     * @return reference to commandline option
     * @throw  std::out_of_range if the commandline option cannot be found
     */
    CommandOption& operator[](const char shortopt);

  private:
    int m_argc;
    char **m_argv;
    std::string m_description;
    CommandOptionList m_cmdlist;
    std::vector<std::string> m_synopsis;
    std::string m_optstring;
    struct option *m_longopts;
};

#endif

/* vim: set et sw=2 ts=2: */