CmdArgs.h
Go to the documentation of this file.
1 // ===========================================================================
2 // Copyright (c) 2011-2012 University of Pennsylvania
3 // Copyright (c) 2013-2016 Andreas Schuh
4 // All rights reserved.
5 //
6 // See COPYING file for license information or visit
7 // https://cmake-basis.github.io/download.html#license
8 // ===========================================================================
9 
10 /**
11  * @file CmdArgs.h
12  * @brief Definition of commonly used command-line arguments.
13  *
14  * This include file mainly redefines the TCLAP command-line argument types
15  * in the namespace of BASIS itself. It only defines commonly used argument
16  * types without template parameters.
17  */
18 
19 #pragma once
20 #ifndef _BASIS_CMDARGS_H
21 #define _BASIS_CMDARGS_H
22 
23 
24 #include "tclap/SwitchArg.h"
25 #include "tclap/MultiSwitchArg.h"
26 #include "tclap/UnlabeledValueArg.h"
27 #include "tclap/UnlabeledMultiArg.h"
28 
29 #include "ValueArg.h"
30 #include "MultiArg.h"
31 
32 #include "tclap/Constraint.h"
33 
34 #include "os/path.h" // isfile(), isdir()
35 
36 
37 namespace basis {
38 
39 
40 /// @addtogroup CxxCmdLine
41 /// @{
42 
43 
44 // ---------------------------------------------------------------------------
45 // common
46 /// Base type of command-line arguments.
47 typedef TCLAP::Arg Arg;
48 
49 // ---------------------------------------------------------------------------
50 // option switches
51 
52 /// Switch to enable/disable option.
54 /// Counts occurrences of option switch.
56 
57 // ---------------------------------------------------------------------------
58 // single argument option
59 
60 // Note: Use full namespace on the left side to help Doxygen to create
61 // the proper references to the BASIS ValueArg class.
62 
63 /// String argument.
65 /// Signed 32-bit integer argument.
67 /// Unsigned 32-bit integer argument.
69 /// Signed 64-bit integer argument.
71 /// Unsigned 64-bit integer argument.
73 /// Alias for Int32Arg.
75 /// Alias for UInt32Arg.
77 /// Floating-point argument.
79 /// Floating-point argument (double precision).
81 
82 // ---------------------------------------------------------------------------
83 // multiple arguments option
84 
85 // Note: Use full namespace on the left side to help Doxygen to create
86 // the proper references to the BASIS MultiArg class.
87 
88 /// String argument (multiple occurrences allowed).
90 /// Signed 32-bit integer argument (multiple occurrences allowed).
92 /// Unsigned 32-bit integer argument (multiple occurrences allowed).
94 /// Signed 64-bit integer argument (multiple occurrences allowed).
96 /// Unsigned 64-bit integer argument (multiple occurrences allowed).
98 /// Floating-point argument (multiple occurrences allowed).
100 /// Floating-point argument (double precision, multiple occurrences allowed).
102 /// Alias for MultiInt32Arg.
104 /// Alias for MultiUInt32Arg.
106 
107 // ---------------------------------------------------------------------------
108 // positional arguments
109 
110 /**
111  * @brief Positional argument.
112  *
113  * Processes only one positional argument. Add the positional arguments in
114  * the right order to the command-line.
115  *
116  * @note The other unlabeled arguments as supported by TCLAP are intentionally
117  * not available through BASIS itself. Any non-string argument should
118  * more likely be a labeled argument, i.e., one with an option flag.
119  */
120 typedef TCLAP::UnlabeledValueArg<std::string> PositionalArg;
121 
122 /**
123  * @brief Positional arguments.
124  *
125  * Use only one positional argument per command-line. Must be the last argument
126  * added to the command-line as it is greedy and will aggregate all remaining
127  * command-line arguments.
128  *
129  * @note The other unlabeled arguments as supported by TCLAP are intentionally
130  * not available through BASIS itself. Any non-string argument should
131  * more likely be a labeled argument, i.e., one with an option flag.
132  */
133 typedef TCLAP::UnlabeledMultiArg<std::string> PositionalArgs;
134 
135 // ===========================================================================
136 // constraints
137 // ===========================================================================
138 
139 // ---------------------------------------------------------------------------
140 // constraints on enumerations
141 // ---------------------------------------------------------------------------
142 
143 /**
144  * @brief Constrains string arguments to allow only predefined values.
145  */
146 typedef TCLAP::ValuesConstraint<std::string> StringValuesConstraint;
147 
148 // ---------------------------------------------------------------------------
149 // constraints on numbers
150 // ---------------------------------------------------------------------------
151 
152 /**
153  * @brief Constrain argument values to negative values.
154  */
155 template<typename T>
156 class NegativeValueConstraint : public TCLAP::Constraint<T>
157 {
158 public:
159  NegativeValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
161  virtual std::string description() const { return "Value must be negative."; }
162  virtual std::string shortID() const { return _typeDesc; }
163  virtual bool check(const T& value) const { return value < 0; }
164 protected:
165  std::string _typeDesc;
166 };
167 
168 /**
169  * @brief Constrain argument values to zero or negative values.
170  */
171 template<typename T>
172 class ZeroOrNegativeValueConstraint : public TCLAP::Constraint<T>
173 {
174 public:
175  ZeroOrNegativeValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
177  virtual std::string description() const { return "Value must be less or equal to zero."; }
178  virtual std::string shortID() const { return _typeDesc; }
179  virtual bool check(const T& value) const { return value <= 0; }
180 protected:
181  std::string _typeDesc;
182 };
183 
184 /**
185  * @brief Constrain argument values to non-zero values.
186  */
187 template<typename T>
188 class NonZeroValueConstraint : public TCLAP::Constraint<T>
189 {
190 public:
191  NonZeroValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
193  virtual std::string description() const { return "Value must not be zero."; }
194  virtual std::string shortID() const { return _typeDesc; }
195  virtual bool check(const T& value) const { return value != 0; }
196 protected:
197  std::string _typeDesc;
198 };
199 
200 /**
201  * @brief Constrain argument values to positive values.
202  */
203 template<typename T>
204 class PositiveValueConstraint : public TCLAP::Constraint<T>
205 {
206 public:
207  PositiveValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
209  virtual std::string description() const { return "Value must be positive."; }
210  virtual std::string shortID() const { return _typeDesc; }
211  virtual bool check(const T& value) const { return value > 0; }
212 protected:
213  std::string _typeDesc;
214 };
215 
216 /**
217  * @brief Constrain argument values to zero or positive values.
218  */
219 template<typename T>
220 class ZeroOrPositiveValueConstraint : public TCLAP::Constraint<T>
221 {
222 public:
223  ZeroOrPositiveValueConstraint(const std::string& typeDesc) : _typeDesc(typeDesc) {}
225  virtual std::string description() const { return "Value must be greater or equal to zero."; }
226  virtual std::string shortID() const { return _typeDesc; }
227  virtual bool check(const T& value) const { return value >= 0; }
228 protected:
229  std::string _typeDesc;
230 };
231 
232 // ---------------------------------------------------------------------------
233 // constraints on paths
234 // ---------------------------------------------------------------------------
235 
236 /**
237  * @brief Constrain argument values to paths of existing files.
238  */
239 class ExistingFileConstraint : public TCLAP::Constraint<std::string>
240 {
241 public:
242  ExistingFileConstraint(const std::string& typeDesc = "<file>") : _typeDesc(typeDesc) {}
244  virtual std::string description() const { return "Value must name an existing file."; }
245  virtual std::string shortID() const { return _typeDesc; }
246  virtual bool check(const std::string& value) const { return os::path::isfile(value); }
247 protected:
248  std::string _typeDesc;
249 };
250 
251 /**
252  * @brief Constrain argument values to paths of existing directories.
253  */
254 class ExistingDirectoryConstraint : public TCLAP::Constraint<std::string>
255 {
256 public:
257  ExistingDirectoryConstraint(const std::string& typeDesc = "<dir>") : _typeDesc(typeDesc) {}
259  virtual std::string description() const { return "Value must name an existing directory."; }
260  virtual std::string shortID() const { return _typeDesc; }
261  virtual bool check(const std::string& value) const { return os::path::isdir(value); }
262 protected:
263  std::string _typeDesc;
264 };
265 
266 
267 /// @}
268 // end of Doxygen group
269 
270 
271 } // namespace basis
272 
273 
274 #endif // _BASIS_CMDARGS_H
basis::MultiArg< int > MultiIntArg
Alias for MultiInt32Arg.
Definition: CmdArgs.h:103
Constrain argument values to paths of existing directories.
Definition: CmdArgs.h:254
virtual std::string shortID() const
Definition: CmdArgs.h:194
virtual bool check(const T &value) const
Definition: CmdArgs.h:227
basis::MultiArg< unsigned long > MultiUInt64Arg
Unsigned 64-bit integer argument (multiple occurrences allowed).
Definition: CmdArgs.h:97
PositiveValueConstraint(const std::string &typeDesc)
Definition: CmdArgs.h:207
basis::ValueArg< unsigned int > UInt32Arg
Unsigned 32-bit integer argument.
Definition: CmdArgs.h:68
virtual std::string shortID() const
Definition: CmdArgs.h:178
virtual bool check(const T &value) const
Definition: CmdArgs.h:179
virtual std::string description() const
Definition: CmdArgs.h:209
An argument that allows multiple values of type T to be specified.
Definition: MultiArg.h:47
virtual bool check(const std::string &value) const
Definition: CmdArgs.h:261
basis::MultiArg< long > MultiInt64Arg
Signed 64-bit integer argument (multiple occurrences allowed).
Definition: CmdArgs.h:95
basis::MultiArg< double > MultiDoubleArg
Floating-point argument (double precision, multiple occurrences allowed).
Definition: CmdArgs.h:101
virtual std::string description() const
Definition: CmdArgs.h:244
TCLAP::SwitchArg SwitchArg
Switch to enable/disable option.
Definition: CmdArgs.h:53
basis::ValueArg< std::string > StringArg
String argument.
Definition: CmdArgs.h:64
Constrain argument values to negative values.
Definition: CmdArgs.h:156
virtual std::string shortID() const
Definition: CmdArgs.h:162
basis::MultiArg< unsigned int > MultiUInt32Arg
Unsigned 32-bit integer argument (multiple occurrences allowed).
Definition: CmdArgs.h:93
basis::ValueArg< float > FloatArg
Floating-point argument.
Definition: CmdArgs.h:78
basis::ValueArg< long > Int64Arg
Signed 64-bit integer argument.
Definition: CmdArgs.h:70
virtual ~ExistingFileConstraint()
Definition: CmdArgs.h:243
ExistingDirectoryConstraint(const std::string &typeDesc="<dir>")
Definition: CmdArgs.h:257
Definition: basis.h:34
virtual std::string shortID() const
Definition: CmdArgs.h:210
basis::ValueArg< unsigned long > UInt64Arg
Unsigned 64-bit integer argument.
Definition: CmdArgs.h:72
TCLAP::UnlabeledMultiArg< std::string > PositionalArgs
Positional arguments.
Definition: CmdArgs.h:133
basis::ValueArg< int > Int32Arg
Signed 32-bit integer argument.
Definition: CmdArgs.h:66
virtual std::string description() const
Definition: CmdArgs.h:193
basis::MultiArg< std::string > MultiStringArg
String argument (multiple occurrences allowed).
Definition: CmdArgs.h:89
bool isfile(const std::string path)
Test whether a given path is the path of an existent file.
Definition: path.cxx:448
virtual ~PositiveValueConstraint()
Definition: CmdArgs.h:208
virtual bool check(const T &value) const
Definition: CmdArgs.h:195
ExistingFileConstraint(const std::string &typeDesc="<file>")
Definition: CmdArgs.h:242
basis::ValueArg< unsigned int > UIntArg
Alias for UInt32Arg.
Definition: CmdArgs.h:76
virtual std::string shortID() const
Definition: CmdArgs.h:226
Constrain argument values to positive values.
Definition: CmdArgs.h:204
virtual std::string description() const
Definition: CmdArgs.h:225
virtual std::string description() const
Definition: CmdArgs.h:161
Constrain argument values to zero or positive values.
Definition: CmdArgs.h:220
virtual ~NegativeValueConstraint()
Definition: CmdArgs.h:160
TCLAP::Arg Arg
Base type of command-line arguments.
Definition: CmdArgs.h:47
virtual std::string description() const
Definition: CmdArgs.h:177
virtual ~NonZeroValueConstraint()
Definition: CmdArgs.h:192
TCLAP::ValuesConstraint< std::string > StringValuesConstraint
Constrains string arguments to allow only predefined values.
Definition: CmdArgs.h:146
virtual std::string shortID() const
Definition: CmdArgs.h:260
File/directory path related functions.
NegativeValueConstraint(const std::string &typeDesc)
Definition: CmdArgs.h:159
NonZeroValueConstraint(const std::string &typeDesc)
Definition: CmdArgs.h:191
Constrain argument values to non-zero values.
Definition: CmdArgs.h:188
Constrain argument values to zero or negative values.
Definition: CmdArgs.h:172
basis::MultiArg< float > MultiFloatArg
Floating-point argument (multiple occurrences allowed).
Definition: CmdArgs.h:99
virtual bool check(const T &value) const
Definition: CmdArgs.h:211
bool isdir(const std::string path)
Test whether a given path is the path of an existent directory.
Definition: path.cxx:462
virtual std::string shortID() const
Definition: CmdArgs.h:245
basis::MultiArg< unsigned int > MultiUIntArg
Alias for MultiUInt32Arg.
Definition: CmdArgs.h:105
basis::ValueArg< int > IntArg
Alias for Int32Arg.
Definition: CmdArgs.h:74
TCLAP::UnlabeledValueArg< std::string > PositionalArg
Positional argument.
Definition: CmdArgs.h:120
Constrain argument values to paths of existing files.
Definition: CmdArgs.h:239
virtual bool check(const std::string &value) const
Definition: CmdArgs.h:246
ZeroOrPositiveValueConstraint(const std::string &typeDesc)
Definition: CmdArgs.h:223
Extends TCLAP&#39;s MultiArg implementation.
TCLAP::MultiSwitchArg MultiSwitchArg
Counts occurrences of option switch.
Definition: CmdArgs.h:55
basis::ValueArg< double > DoubleArg
Floating-point argument (double precision).
Definition: CmdArgs.h:80
virtual bool check(const T &value) const
Definition: CmdArgs.h:163
ZeroOrNegativeValueConstraint(const std::string &typeDesc)
Definition: CmdArgs.h:175
Extends TCLAP&#39;s ValueArg implementation.
basis::MultiArg< int > MultiInt32Arg
Signed 32-bit integer argument (multiple occurrences allowed).
Definition: CmdArgs.h:91
An argument that allows multiple values of type T to be specified.
Definition: ValueArg.h:47
virtual std::string description() const
Definition: CmdArgs.h:259