summaryrefslogtreecommitdiff
path: root/qpid/cpp/examples/messaging/OptionParser.cpp
blob: 41a71af49a18bbd78df0b5382a70a044f88ab157 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
#include "OptionParser.h"
#include <qpid/types/Exception.h>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdlib>

class Option
{
  public:
    Option(const std::string& name, const std::string& description);
    virtual ~Option() {}
    virtual void setValue(const std::string&) = 0;
    virtual bool isValueExpected() = 0;
    bool match(const std::string&);
    std::ostream& print(std::ostream& out);
  private:
    std::string longName;
    std::string shortName;
    std::string description;
    std::ostream& printNames(std::ostream& out);
  friend class OptionParser;
};

class StringOption : public Option
{
  public:
    StringOption(const std::string& name, const std::string& description, std::string& v) : Option(name, description), value(v) {}
    void setValue(const std::string& v) { value = v; }
    bool isValueExpected() { return true; }
  private:
    std::string& value;
};

class IntegerOption : public Option
{
  public:
    IntegerOption(const std::string& name, const std::string& description, int& v) : Option(name, description), value(v) {}
    void setValue(const std::string& v) { value = atoi(v.c_str()); }
    bool isValueExpected() { return true; }
  private:
    int& value;
};

class BooleanOption : public Option
{
  public:
    BooleanOption(const std::string& name, const std::string& description, bool& v) : Option(name, description), value(v) {}
    void setValue(const std::string&) { value = true; }
    bool isValueExpected() { return false; }
  private:
    bool& value;
};

class MultiStringOption : public Option
{
  public:
    MultiStringOption(const std::string& name, const std::string& description, std::vector<std::string>& v) : Option(name, description), value(v) {}
    void setValue(const std::string& v) { value.push_back(v); }
    bool isValueExpected() { return true; }
  private:
    std::vector<std::string>& value;
};

class OptionMatch
{
  public:
    OptionMatch(const std::string& argument);
    bool operator()(Option* option);
    bool isOption();
  private:
    std::string name;
};

Option::Option(const std::string& name, const std::string& desc) : description(desc)
{
    std::string::size_type i = name.find(",");
    if (i != std::string::npos) {
        longName = name.substr(0, i);
        if (i + 1 < name.size())
            shortName = name.substr(i+1);
    } else {
        longName = name;
    }
}

bool Option::match(const std::string& name)
{
    return name == longName || name == shortName;
}

std::ostream& Option::printNames(std::ostream& out)
{
    if (shortName.size()) {
        out << "-" << shortName;
        if (isValueExpected()) out << " VALUE";
        out << ", --" << longName;
        if (isValueExpected()) out << " VALUE";
    } else {
        out << "--" << longName;
        if (isValueExpected()) out << " VALUE";
    }
    return out;
}

std::ostream& Option::print(std::ostream& out)
{
    std::stringstream names;
    printNames(names);
    out << std::setw(30) << std::left << names.str() << description << std::endl;
    return out;
}

std::vector<std::string>& OptionParser::getArguments() { return arguments; }

void OptionParser::add(Option* option)
{
    options.push_back(option);
}

void OptionParser::add(const std::string& name, std::string& value, const std::string& description)
{
    add(new StringOption(name, description, value));
}
void OptionParser::add(const std::string& name, int& value, const std::string& description)
{
    add(new IntegerOption(name, description, value));
}
void OptionParser::add(const std::string& name, bool& value, const std::string& description)
{
    add(new BooleanOption(name, description, value));
}
void OptionParser::add(const std::string& name, std::vector<std::string>& value, const std::string& description)
{
    add(new MultiStringOption(name, description, value));
}

OptionMatch::OptionMatch(const std::string& argument)
{
    if (argument.find("--") == 0) {
        name = argument.substr(2);
    } else if (argument.find("-") == 0) {
        name = argument.substr(1);
    }
}

bool OptionMatch::operator()(Option* option)
{
    return option->match(name);
}

bool OptionMatch::isOption()
{
    return name.size() > 0;
}

OptionParser::OptionParser(const std::string& s, const std::string& d) : summary(s), description(d), help(false)
{
    add("help,h", help, "show this message");
}

Option* OptionParser::getOption(const std::string& argument)
{
    OptionMatch match(argument);
    if (match.isOption()) {
        Options::iterator i = std::find_if(options.begin(), options.end(), match);
        if (i == options.end()) {
            std::stringstream error;
            error << "Unrecognised option: " << argument;
            throw OptionsError(error.str());
        } else {
            return *i;
        }
    } else {
        return 0;
    }
}

void OptionParser::error(const std::string& message)
{
    std::cout << summary << std::endl << std::endl;
    std::cerr << "Error: " << message << "; try --help for more information" << std::endl;
}

bool OptionParser::parse(int argc, char** argv)
{
    try {
        for (int i = 1; i < argc; ++i) {
            std::string argument = argv[i];
            Option* o = getOption(argument);
            if (o) {
                if (o->isValueExpected()) {
                    if (i + 1 < argc) {
                        o->setValue(argv[++i]);
                    } else {
                        std::stringstream error;
                        error << "Value expected for option " << o->longName;
                        throw OptionsError(error.str());
                    }
                } else {
                    o->setValue("");
                }
            } else {
                arguments.push_back(argument);
            }
        }
        if (help) {
            std::cout << summary << std::endl << std::endl;
            std::cout << description << std::endl << std::endl;
            std::cout << "Options: " << std::endl;
            for (Options::iterator i = options.begin(); i != options.end(); ++i) {
                (*i)->print(std::cout);
            }
            return false;
        } else {
            return true;
        }
    } catch (const std::exception& e) {
        error(e.what());
        return false;
    }
}


OptionParser::~OptionParser()
{
    for (Options::iterator i = options.begin(); i != options.end(); ++i) {
        delete *i;
    }
}