OpenASIP  2.0
CmdLineOptionParser.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file CmdLineOptionParser.hh
26  *
27  * Declaration of CmdLineOptionParser classes.
28  *
29  * @author Jussi Nykänen 2003 (nykanen-no.spam-cs.tut.fi)
30  * @author Jari Mäntyneva 2006 (jari.mantyneva-no.spam-tut.fi)
31  * @author Henry Linjamäki 2017 (henry.linjamaki-no.spam-tut.fi)
32  * @note reviewed 3 December 2003 by jn, kl, ao
33  */
34 
35 #ifndef TTA_CMD_LINE_OPTION_PARSER_HH
36 #define TTA_CMD_LINE_OPTION_PARSER_HH
37 
38 #include <string>
39 #include <vector>
40 #include "Exception.hh"
41 
42 class OptionValue;
43 
44 //////////////////////////////////////////////////////////////////////////////
45 // CmdLineOptionParser
46 //////////////////////////////////////////////////////////////////////////////
47 
48 /**
49  * Abstract base class for modeling command line options and their built-in
50  * mini-parser.
51  *
52  * All option types have a set of properties that identify the option: name,
53  * description and short name (alias). Possible concrete types of options
54  * are: Boolean, real, integer, and character string.
55  */
57 public:
58 
60  std::string name,
61  std::string desc,
62  std::string alias,
63  bool hidden = false);
64  virtual ~CmdLineOptionParser();
65 
66  virtual OptionValue* copy() const = 0;
67 
68  std::string longName() const;
69  std::string shortName() const;
70  std::string description() const;
71 
72  /// Pure virtual function that parses the value of option.
73  virtual bool parseValue(std::string arguments, std::string prefix) = 0;
74 
75  bool isHidden() { return hidden_; }
76  bool isDefined();
77 
78  virtual int integer(int index = 0) const;
79  virtual unsigned unsignedInteger(int index = 0) const;
80  virtual std::string String(int index = 0) const;
81  virtual double real() const;
82  virtual bool isFlagOn() const;
83  virtual bool isFlagOff() const;
84  virtual int listSize() const;
85 
86 protected:
87  void setDefined();
88 
89 private:
90 
91  /// Copying not allowed.
93  /// Assignment not allowed.
95 
96  /// The full name of an option.
97  std::string longName_;
98  /// The optional alias (shorter name).
99  std::string shortName_;
100  /// The description of option.
101  std::string desc_;
102  /// The hidden flag. If set, no entry is printed in the normal help text
103  bool hidden_ = false;
104 
105  /// Is the value of this option set in the parsed command line?
106  bool defined_;
107 };
108 
109 
110 //////////////////////////////////////////////////////////////////////////////
111 // IntegerCmdLineOptionParser
112 //////////////////////////////////////////////////////////////////////////////
113 
114 /**
115  * Models an option that has an integer value.
116  */
118 public:
120  std::string name,
121  std::string desc,
122  std::string alias = "");
123  virtual ~IntegerCmdLineOptionParser();
124 
125  virtual OptionValue* copy() const;
126 
127  virtual bool parseValue(std::string arguments, std::string prefix);
128  virtual int integer(int index = 0) const;
129 
130 private:
131  /// Copying not allowed.
133  /// Assignment not allowed.
135 
136  /// The value of option.
137  int value_;
138 };
139 
140 //////////////////////////////////////////////////////////////////////////////
141 // UnsignedIntegerCmdLineOptionParser
142 //////////////////////////////////////////////////////////////////////////////
143 
144 /**
145  * Models an option that has an unsigned integer value.
146  */
148 public:
150  std::string name,
151  std::string desc,
152  std::string alias = "");
154 
155  virtual OptionValue* copy() const;
156 
157  virtual bool parseValue(std::string arguments, std::string prefix);
158  virtual unsigned unsignedInteger(int index = 0) const;
159 
160 private:
161  /// Copying not allowed.
164  /// Assignment not allowed.
167 
168  /// The value of option.
169  unsigned value_;
170 };
171 
172 
173 //////////////////////////////////////////////////////////////////////////////
174 // StringCmdLineOptionParser
175 //////////////////////////////////////////////////////////////////////////////
176 
177 /**
178  * CmdLineOptionParser that has a string as a value.
179  */
181 public:
183  std::string name,
184  std::string desc,
185  std::string alias = "");
186  virtual ~StringCmdLineOptionParser();
187 
188  virtual OptionValue* copy() const;
189 
190  virtual bool parseValue(std::string arguments, std::string prefix);
191  virtual std::string String(int index = 0) const;
192 
193 private:
194  /// Copying not allowed.
196  /// Assignment not allowed.
198 
199  /// The value of the option.
200  std::string value_;
201 };
202 
203 //////////////////////////////////////////////////////////////////////////////
204 // OptionalStringCmdLineOptionParser
205 //////////////////////////////////////////////////////////////////////////////
206 
207 /**
208  * CmdLineOptionParser that acts as flag with optional string value.
209  */
211 public:
213  std::string name,
214  std::string desc,
215  std::string alias = "");
217 
218  virtual OptionValue* copy() const;
219 
220  virtual bool parseValue(std::string arguments, std::string prefix);
221  virtual std::string String(int index = 0) const;
222  virtual bool isFlagOn() const;
223  virtual bool isFlagOff() const;
224 
225 private:
226  /// Copying not allowed.
229  /// Assignment not allowed.
232 
233  /// The value of the option.
234  std::string value_;
235  /// The flag status.
236  bool flag_;
237 };
238 
239 //////////////////////////////////////////////////////////////////////////////
240 // RealCmdLineOptionParser
241 //////////////////////////////////////////////////////////////////////////////
242 
243 /**
244  * CmdLineOptionParser that has a real value.
245  */
247 public:
249  std::string name,
250  std::string desc,
251  std::string alias = "");
252  virtual ~RealCmdLineOptionParser();
253 
254  virtual OptionValue* copy() const;
255 
256  virtual bool parseValue(std::string arguments, std::string prefix);
257  virtual double real() const;
258 
259 private:
260  /// Copying not allowed.
262  /// Assignment not allowed.
264 
265  /// The value of the option.
266  double value_;
267 };
268 
269 //////////////////////////////////////////////////////////////////////////////
270 // BoolCmdLineOptionParser
271 //////////////////////////////////////////////////////////////////////////////
272 
273 /**
274  * CmdLineOptionParser that has a boolean value.
275  *
276  * This option is also called 'flag'.
277  */
279 public:
281  std::string name,
282  std::string desc,
283  std::string alias = "",
284  bool hidden = false);
285  virtual ~BoolCmdLineOptionParser();
286 
287  virtual OptionValue* copy() const;
288 
289  virtual bool parseValue(std::string arguments, std::string prefix);
290  virtual bool isFlagOn() const;
291  virtual bool isFlagOff() const;
292 
293 private:
294  /// Copying not allowed.
296  /// Assignment not allowed.
298 
299  /// The value of option.
300  bool value_;
301 };
302 
303 //////////////////////////////////////////////////////////////////////////////
304 // IntegerListCmdLineOptionParser
305 //////////////////////////////////////////////////////////////////////////////
306 
307 /**
308  * CmdLineOptionParser that has a list of integers as value.
309  */
311 public:
313  std::string name,
314  std::string desc,
315  std::string alias = "");
317 
318  virtual OptionValue* copy() const;
319 
320  virtual bool parseValue(std::string arguments, std::string prefix);
321 
322  virtual int integer(int index = 0) const;
323  virtual int listSize() const;
324 
325 private:
326  /// Copying not allowed.
328  /// Assignment not allowed.
331 
332  /// The values in integer list.
333  std::vector<int> values_;
334 };
335 
336 
337 //////////////////////////////////////////////////////////////////////////////
338 // StringListCmdLineOptionParser
339 //////////////////////////////////////////////////////////////////////////////
340 
341 /**
342  * CmdLineOptionParser that has a list of strings as value.
343  */
345 public:
347  std::string name,
348  std::string desc,
349  std::string alias = "");
351 
352  virtual OptionValue* copy() const;
353 
354  virtual bool parseValue(std::string arguments, std::string prefix);
355 
356  virtual std::string String(int index = 0) const;
357  virtual int listSize() const;
358 
359 private:
360  /// Copying not allowed.
362  /// Assignment not allowed.
365 
366  /// The values in string list.
367  std::vector<std::string> values_;
368 };
369 
370 #include "CmdLineOptionParser.icc"
371 
372 #endif
UnsignedIntegerCmdLineOptionParser::UnsignedIntegerCmdLineOptionParser
UnsignedIntegerCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
Definition: CmdLineOptionParser.cc:269
OptionalStringCmdLineOptionParser
Definition: CmdLineOptionParser.hh:210
OptionalStringCmdLineOptionParser::isFlagOff
virtual bool isFlagOff() const
Definition: CmdLineOptionParser.cc:492
StringListCmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)
Definition: CmdLineOptionParser.cc:837
UnsignedIntegerCmdLineOptionParser::operator=
UnsignedIntegerCmdLineOptionParser & operator=(const UnsignedIntegerCmdLineOptionParser &)
Assignment not allowed.
StringListCmdLineOptionParser::StringListCmdLineOptionParser
StringListCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
Definition: CmdLineOptionParser.cc:799
CmdLineOptionParser::unsignedInteger
virtual unsigned unsignedInteger(int index=0) const
Definition: CmdLineOptionParser.cc:90
IntegerCmdLineOptionParser::operator=
IntegerCmdLineOptionParser & operator=(const IntegerCmdLineOptionParser &)
Assignment not allowed.
Exception.hh
CmdLineOptionParser::isDefined
bool isDefined()
Definition: CmdLineOptionParser.cc:169
RealCmdLineOptionParser::copy
virtual OptionValue * copy() const
Definition: CmdLineOptionParser.cc:529
UnsignedIntegerCmdLineOptionParser::unsignedInteger
virtual unsigned unsignedInteger(int index=0) const
Definition: CmdLineOptionParser.cc:331
OptionalStringCmdLineOptionParser::isFlagOn
virtual bool isFlagOn() const
Definition: CmdLineOptionParser.cc:481
BoolCmdLineOptionParser::value_
bool value_
The value of option.
Definition: CmdLineOptionParser.hh:300
StringListCmdLineOptionParser::String
virtual std::string String(int index=0) const
Definition: CmdLineOptionParser.cc:877
IntegerListCmdLineOptionParser::~IntegerListCmdLineOptionParser
virtual ~IntegerListCmdLineOptionParser()
Definition: CmdLineOptionParser.cc:690
CmdLineOptionParser::setDefined
void setDefined()
Definition: CmdLineOptionParser.cc:159
CmdLineOptionParser::isHidden
bool isHidden()
Definition: CmdLineOptionParser.hh:75
BoolCmdLineOptionParser::BoolCmdLineOptionParser
BoolCmdLineOptionParser(std::string name, std::string desc, std::string alias="", bool hidden=false)
Definition: CmdLineOptionParser.cc:591
StringListCmdLineOptionParser::~StringListCmdLineOptionParser
virtual ~StringListCmdLineOptionParser()
Definition: CmdLineOptionParser.cc:808
CmdLineOptionParser::isFlagOn
virtual bool isFlagOn() const
Definition: CmdLineOptionParser.cc:126
UnsignedIntegerCmdLineOptionParser::value_
unsigned value_
The value of option.
Definition: CmdLineOptionParser.hh:169
StringCmdLineOptionParser::operator=
StringCmdLineOptionParser & operator=(const StringCmdLineOptionParser &)
Assignment not allowed.
CmdLineOptionParser::shortName
std::string shortName() const
CmdLineOptionParser::isFlagOff
virtual bool isFlagOff() const
Definition: CmdLineOptionParser.cc:138
CmdLineOptionParser::operator=
CmdLineOptionParser & operator=(const CmdLineOptionParser &)
Assignment not allowed.
StringCmdLineOptionParser::~StringCmdLineOptionParser
virtual ~StringCmdLineOptionParser()
Definition: CmdLineOptionParser.cc:356
IntegerListCmdLineOptionParser
Definition: CmdLineOptionParser.hh:310
RealCmdLineOptionParser::RealCmdLineOptionParser
RealCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
Definition: CmdLineOptionParser.cc:507
CmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)=0
Pure virtual function that parses the value of option.
StringCmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)
Definition: CmdLineOptionParser.cc:382
UnsignedIntegerCmdLineOptionParser::copy
virtual OptionValue * copy() const
Definition: CmdLineOptionParser.cc:283
IntegerListCmdLineOptionParser::operator=
IntegerListCmdLineOptionParser & operator=(const IntegerListCmdLineOptionParser &)
Assignment not allowed.
OptionalStringCmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)
Definition: CmdLineOptionParser.cc:453
CmdLineOptionParser::listSize
virtual int listSize() const
Definition: CmdLineOptionParser.cc:150
CmdLineOptionParser::copy
virtual OptionValue * copy() const =0
CmdLineOptionParser
Definition: CmdLineOptionParser.hh:56
CmdLineOptionParser::hidden_
bool hidden_
The hidden flag. If set, no entry is printed in the normal help text.
Definition: CmdLineOptionParser.hh:103
CmdLineOptionParser::CmdLineOptionParser
CmdLineOptionParser(std::string name, std::string desc, std::string alias, bool hidden=false)
Definition: CmdLineOptionParser.cc:62
StringCmdLineOptionParser::String
virtual std::string String(int index=0) const
Definition: CmdLineOptionParser.cc:403
BoolCmdLineOptionParser
Definition: CmdLineOptionParser.hh:278
OptionalStringCmdLineOptionParser::operator=
OptionalStringCmdLineOptionParser & operator=(const OptionalStringCmdLineOptionParser &)
Assignment not allowed.
RealCmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)
Definition: CmdLineOptionParser.cc:543
BoolCmdLineOptionParser::isFlagOff
virtual bool isFlagOff() const
Definition: CmdLineOptionParser.cc:666
RealCmdLineOptionParser::value_
double value_
The value of the option.
Definition: CmdLineOptionParser.hh:266
IntegerCmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)
Definition: CmdLineOptionParser.cc:219
CmdLineOptionParser::longName
std::string longName() const
OptionalStringCmdLineOptionParser::flag_
bool flag_
The flag status.
Definition: CmdLineOptionParser.hh:236
IntegerCmdLineOptionParser::IntegerCmdLineOptionParser
IntegerCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
Definition: CmdLineOptionParser.cc:184
StringListCmdLineOptionParser::listSize
virtual int listSize() const
Definition: CmdLineOptionParser.cc:889
CmdLineOptionParser::real
virtual double real() const
Definition: CmdLineOptionParser.cc:114
CmdLineOptionParser::longName_
std::string longName_
The full name of an option.
Definition: CmdLineOptionParser.hh:97
IntegerCmdLineOptionParser::~IntegerCmdLineOptionParser
virtual ~IntegerCmdLineOptionParser()
Definition: CmdLineOptionParser.cc:193
CmdLineOptionParser::String
virtual std::string String(int index=0) const
Definition: CmdLineOptionParser.cc:102
OptionalStringCmdLineOptionParser::OptionalStringCmdLineOptionParser
OptionalStringCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
Definition: CmdLineOptionParser.cc:419
BoolCmdLineOptionParser::~BoolCmdLineOptionParser
virtual ~BoolCmdLineOptionParser()
Definition: CmdLineOptionParser.cc:602
BoolCmdLineOptionParser::operator=
BoolCmdLineOptionParser & operator=(const BoolCmdLineOptionParser &)
Assignment not allowed.
CmdLineOptionParser::description
std::string description() const
OptionalStringCmdLineOptionParser::~OptionalStringCmdLineOptionParser
virtual ~OptionalStringCmdLineOptionParser()
Definition: CmdLineOptionParser.cc:427
StringListCmdLineOptionParser
Definition: CmdLineOptionParser.hh:344
CmdLineOptionParser::desc_
std::string desc_
The description of option.
Definition: CmdLineOptionParser.hh:101
BoolCmdLineOptionParser::copy
virtual OptionValue * copy() const
Definition: CmdLineOptionParser.cc:614
CmdLineOptionParser::~CmdLineOptionParser
virtual ~CmdLineOptionParser()
Definition: CmdLineOptionParser.cc:74
UnsignedIntegerCmdLineOptionParser::~UnsignedIntegerCmdLineOptionParser
virtual ~UnsignedIntegerCmdLineOptionParser()
Definition: CmdLineOptionParser.cc:279
StringListCmdLineOptionParser::operator=
StringListCmdLineOptionParser & operator=(const StringListCmdLineOptionParser &)
Assignment not allowed.
StringCmdLineOptionParser::copy
virtual OptionValue * copy() const
Definition: CmdLineOptionParser.cc:368
OptionValue
Definition: OptionValue.hh:51
StringCmdLineOptionParser::value_
std::string value_
The value of the option.
Definition: CmdLineOptionParser.hh:200
UnsignedIntegerCmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)
Definition: CmdLineOptionParser.cc:297
BoolCmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)
Definition: CmdLineOptionParser.cc:628
OptionalStringCmdLineOptionParser::copy
virtual OptionValue * copy() const
Definition: CmdLineOptionParser.cc:439
CmdLineOptionParser::shortName_
std::string shortName_
The optional alias (shorter name).
Definition: CmdLineOptionParser.hh:99
CmdLineOptionParser::integer
virtual int integer(int index=0) const
Definition: CmdLineOptionParser.cc:84
CmdLineOptionParser.icc
RealCmdLineOptionParser
Definition: CmdLineOptionParser.hh:246
UnsignedIntegerCmdLineOptionParser
Definition: CmdLineOptionParser.hh:147
CmdLineOptionParser::defined_
bool defined_
Is the value of this option set in the parsed command line?
Definition: CmdLineOptionParser.hh:106
BoolCmdLineOptionParser::isFlagOn
virtual bool isFlagOn() const
Definition: CmdLineOptionParser.cc:655
OptionalStringCmdLineOptionParser::value_
std::string value_
The value of the option.
Definition: CmdLineOptionParser.hh:234
IntegerListCmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)
Definition: CmdLineOptionParser.cc:719
StringListCmdLineOptionParser::values_
std::vector< std::string > values_
The values in string list.
Definition: CmdLineOptionParser.hh:367
RealCmdLineOptionParser::real
virtual double real() const
Definition: CmdLineOptionParser.cc:576
IntegerListCmdLineOptionParser::listSize
virtual int listSize() const
Definition: CmdLineOptionParser.cc:784
OptionalStringCmdLineOptionParser::String
virtual std::string String(int index=0) const
Definition: CmdLineOptionParser.cc:469
RealCmdLineOptionParser::~RealCmdLineOptionParser
virtual ~RealCmdLineOptionParser()
Definition: CmdLineOptionParser.cc:517
IntegerListCmdLineOptionParser::values_
std::vector< int > values_
The values in integer list.
Definition: CmdLineOptionParser.hh:333
IntegerListCmdLineOptionParser::copy
virtual OptionValue * copy() const
Definition: CmdLineOptionParser.cc:703
IntegerCmdLineOptionParser::integer
virtual int integer(int index=0) const
Definition: CmdLineOptionParser.cc:253
StringCmdLineOptionParser
Definition: CmdLineOptionParser.hh:180
RealCmdLineOptionParser::operator=
RealCmdLineOptionParser & operator=(const RealCmdLineOptionParser &)
Assignment not allowed.
IntegerListCmdLineOptionParser::IntegerListCmdLineOptionParser
IntegerListCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
Definition: CmdLineOptionParser.cc:681
IntegerCmdLineOptionParser::value_
int value_
The value of option.
Definition: CmdLineOptionParser.hh:137
IntegerListCmdLineOptionParser::integer
virtual int integer(int index=0) const
Definition: CmdLineOptionParser.cc:772
StringCmdLineOptionParser::StringCmdLineOptionParser
StringCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
Definition: CmdLineOptionParser.cc:347
StringListCmdLineOptionParser::copy
virtual OptionValue * copy() const
Definition: CmdLineOptionParser.cc:821
IntegerCmdLineOptionParser::copy
virtual OptionValue * copy() const
Definition: CmdLineOptionParser.cc:205
IntegerCmdLineOptionParser
Definition: CmdLineOptionParser.hh:117