CmdLine.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 basis/CmdLine.h
12  * @brief Manages command line definition and parsing of arguments.
13  *
14  * @ingroup CxxCmdLine
15  */
16 
17 #pragma once
18 #ifndef _BASIS_CMDLINE_H
19 #define _BASIS_CMDLINE_H
20 
21 
22 #include "tclap/CmdLine.h" // TCLAP implementations
23 #include "CmdArgs.h" // commonly used arguments
24 
25 
26 namespace basis {
27 
28 
29 /**
30  * @brief Manages command line definition and parsing of arguments.
31  *
32  * Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br />
33  * See http://www.rad.upenn.edu/sbia/software/license.html or COPYING file.
34  *
35  * @ingroup CxxCmdLine
36  */
37 class CmdLine : public TCLAP::CmdLine
38 {
39  // -----------------------------------------------------------------------
40  // XorHandler
41 protected:
42 
43  /**
44  * @brief Handles lists of Arg's that are to be XOR'd on the command-line.
45  *
46  * This subclass of the TCLAP::XorHandler overloads the check() method such
47  * that XOR'd arguments where none of the arguments is required are handled
48  * correctly. The TCLA::XorHandler and TCLAP::CmdLine implementations imply
49  * that all XOR'd arguments are required, i.e., that one of the mutual
50  * exclusive arguments need to be specified. The sbia::basis::XorHandler and
51  * sbia::basis::CmdLine implementations, on the other side, do not require
52  * that any of the XOR'd arguments be given on the command-line if none of
53  * the XOR'd arguments is required.
54  */
55  class XorHandler : public TCLAP::XorHandler
56  {
57  public:
58 
59  /**
60  * @brief Constructor.
61  */
63 
64  /**
65  * @brief Checks whether the specified Arg is in one of the xor lists.
66  *
67  * If the argument does match one, this function returns the size of the xor
68  * list that the Arg matched if the Arg is required. If the Arg matches,
69  * then it also sets the rest of the Arg's in the list.
70  *
71  * @param a The Arg to be checked.
72  */
73  int check(const Arg* a)
74  {
75  int n = TCLAP::XorHandler::check(a);
76  return a->isRequired() ? n : 0;
77  }
78 
79  }; // class XorHandler
80 
81  // -----------------------------------------------------------------------
82  // construction / destruction
83 public:
84 
85  /**
86  * @brief Constructor.
87  *
88  * @param [in] name Program name. Should be a constant string which
89  * helps to identify the program, not the name of
90  * the executable as determined at runtime.
91  * @param [in] project Name of project this program belongs to.
92  * @param [in] description Program description.
93  * @param [in] example Usage example.
94  * @param [in] version Program version.
95  * @param [in] copyright Copyright notice.
96  * @param [in] license License information.
97  * @param [in] contact Contact information.
98  * @param [in] stdargs Enable/disable handling of standard arguments.
99  */
100  CmdLine(const std::string& name,
101  const std::string& project,
102  const std::string& description,
103  const std::string& example,
104  const std::string& version,
105  const std::string& copyright =
106  "Copyright (c) University of Pennsylvania."
107  " All rights reserved.",
108  const std::string& license =
109  "See http://www.rad.upenn.edu/sbia/software/license.html"
110  " or COPYING file.",
111  const std::string& contact =
112  "SBIA Group <sbia-software at uphs.upenn.edu>",
113  bool stdargs = true);
114 
115  /**
116  * @brief Constructor.
117  *
118  * @param [in] name Program name. Should be a constant string which
119  * helps to identify the program, not the name of
120  * the executable as determined at runtime.
121  * @param [in] project Name of project this program belongs to.
122  * @param [in] description Program description.
123  * @param [in] examples Usage examples.
124  * @param [in] version Program version.
125  * @param [in] copyright Copyright notice.
126  * @param [in] license License information.
127  * @param [in] contact Contact information.
128  * @param [in] stdargs Enable/disable handling of standard arguments.
129  */
130  CmdLine(const std::string& name,
131  const std::string& project,
132  const std::string& description,
133  const std::vector<std::string>& examples,
134  const std::string& version,
135  const std::string& copyright =
136  "Copyright (c) University of Pennsylvania."
137  " All rights reserved.",
138  const std::string& license =
139  "See http://www.rad.upenn.edu/sbia/software/license.html"
140  " or COPYING file.",
141  const std::string& contact =
142  "SBIA Group <sbia-software at uphs.upenn.edu>",
143  bool stdargs = true);
144 
145  /**
146  * @brief Destructor.
147  */
148  virtual ~CmdLine() { }
149 
150  // -----------------------------------------------------------------------
151  // command arguments
152 public:
153 
154  /**
155  * @brief Adds an argument to the list of arguments to be parsed.
156  *
157  * @param [in] a Argument to be added.
158  */
159  void add(Arg& a);
160 
161  /**
162  * @brief An alternative add. Functionally identical.
163  *
164  * @param [in] a Argument to be added.
165  */
166  void add(Arg* a);
167 
168  /**
169  * @brief Add two Args that will be xor'd.
170  *
171  * If this method is used, add does not need to be called.
172  *
173  * @param [in] a Argument to be added and xor'd.
174  * @param [in] b Argument to be added and xor'd.
175  */
176  void xorAdd(Arg& a, Arg& b);
177 
178  /**
179  * @brief Add a list of arguments that will be xor'd.
180  *
181  * If this method is used, add does not need to be called.
182  *
183  * @param [in] xors List of Args to be added and xor'd.
184  */
185  void xorAdd(std::vector<Arg*>& xors);
186 
187  // -----------------------------------------------------------------------
188  // help / version
189 public:
190 
191  /**
192  * @brief Print short help, i.e., usage information.
193  */
194  void print_usage() const;
195 
196  /**
197  * @brief Print help.
198  */
199  void print_help() const;
200 
201  /**
202  * @brief Print version information.
203  */
204  void print_version() const;
205 
206  // -----------------------------------------------------------------------
207  // parse command-line arguments
208 public:
209 
210  /**
211  * @brief Parses the command line.
212  *
213  * @param [in] argc Number of arguments.
214  * @param [in] argv Array of arguments.
215  */
216  void parse(int argc, const char* const* argv);
217 
218  /**
219  * @brief Parses the command line.
220  *
221  * @param [in] args A vector of strings representing the args.
222  * args[0] is still the program name.
223  */
224  void parse(std::vector<std::string>& args);
225 
226  // -----------------------------------------------------------------------
227  // accessors
228 public:
229 
230  /**
231  * @brief Get name of program.
232  *
233  * @returns Name of program this command-line object belongs to.
234  */
235  std::string& getProgramName() { return _name; }
236 
237  /**
238  * @brief Get name of project the program belongs to.
239  *
240  * @returns Name of project this program belongs to.
241  */
242  std::string& getProjectName() { return _project; }
243 
244  /**
245  * @brief Get program description.
246  *
247  * @returns Description of program this command-line object belongs to.
248  */
249  std::string& getDescription() { return _message; }
250 
251  /**
252  * @brief Get usage example.
253  *
254  * @returns Example command-line usage.
255  */
256  std::vector<std::string>& getExamples() { return _examples; }
257 
258  /**
259  * @brief Get copyright notice.
260  *
261  * @return Copyright information of program.
262  */
263  std::string& getCopyright() { return _copyright; }
264 
265  /**
266  * @brief Get license information.
267  *
268  * @returns License information of program.
269  */
270  std::string& getLicense() { return _license; }
271 
272  /**
273  * @brief Get contact information.
274  *
275  * @returns Contact information.
276  */
277  std::string& getContact() { return _contact; }
278 
279  /**
280  * @brief Get handler of XOR'd arguments.
281  */
283 
284  // -----------------------------------------------------------------------
285  // helpers
286 protected:
287 
288  /**
289  * @brief Set up command-line object.
290  */
291  void setup(bool stdargs);
292 
293  // -----------------------------------------------------------------------
294  // unsupported
295 private:
296 
297  CmdLine(const CmdLine&); ///< Intentionally not implemented.
298  CmdLine& operator=(const CmdLine&); ///< Intentionally not implemented.
299 
300  // -----------------------------------------------------------------------
301  // member variables
302 protected:
303 
304  XorHandler _xorHandler; ///< Customized XorHandler.
305  std::string _name; ///< Program name.
306  std::string _project; ///< Name of project.
307  std::vector<std::string> _examples; ///< Program usage example.
308  std::string _copyright; ///< Program copyright.
309  std::string _license; ///< Program license.
310  std::string _contact; ///< Contact information.
311 
312 }; // class CmdLine
313 
314 
315 } // namespace basis
316 
317 
318 #endif // _BASIS_CMDLINE_H
void xorAdd(Arg &a, Arg &b)
Add two Args that will be xor&#39;d.
Definition: CmdLine.cxx:893
std::vector< std::string > & getExamples()
Get usage example.
Definition: CmdLine.h:256
void setup(bool stdargs)
Set up command-line object.
Definition: CmdLine.cxx:818
XorHandler _xorHandler
Customized XorHandler.
Definition: CmdLine.h:304
std::string & getProjectName()
Get name of project the program belongs to.
Definition: CmdLine.h:242
void parse(int argc, const char *const *argv)
Parses the command line.
Definition: CmdLine.cxx:937
void print_usage() const
Print short help, i.e., usage information.
Definition: CmdLine.cxx:902
std::string _name
Program name.
Definition: CmdLine.h:305
Definition: basis.h:34
std::string _project
Name of project.
Definition: CmdLine.h:306
Definition of commonly used command-line arguments.
std::string _copyright
Program copyright.
Definition: CmdLine.h:308
CmdLine(const std::string &name, const std::string &project, const std::string &description, const std::string &example, const std::string &version, const std::string &copyright="Copyright (c) University of Pennsylvania." " All rights reserved.", const std::string &license="See http://www.rad.upenn.edu/sbia/software/license.html" " or COPYING file.", const std::string &contact="SBIA Group <sbia-software at uphs.upenn.edu>", bool stdargs=true)
Constructor.
Definition: CmdLine.cxx:772
std::string & getDescription()
Get program description.
Definition: CmdLine.h:249
int check(const Arg *a)
Checks whether the specified Arg is in one of the xor lists.
Definition: CmdLine.h:73
TCLAP::Arg Arg
Base type of command-line arguments.
Definition: CmdArgs.h:47
Manages command line definition and parsing of arguments.
Definition: CmdLine.h:37
XorHandler()
Constructor.
Definition: CmdLine.h:62
std::string _contact
Contact information.
Definition: CmdLine.h:310
std::string _license
Program license.
Definition: CmdLine.h:309
XorHandler & getXorHandler()
Get handler of XOR&#39;d arguments.
Definition: CmdLine.h:282
Handles lists of Arg&#39;s that are to be XOR&#39;d on the command-line.
Definition: CmdLine.h:55
void add(Arg &a)
Adds an argument to the list of arguments to be parsed.
Definition: CmdLine.cxx:881
std::string & getContact()
Get contact information.
Definition: CmdLine.h:277
void print_help() const
Print help.
Definition: CmdLine.cxx:908
std::vector< std::string > _examples
Program usage example.
Definition: CmdLine.h:307
std::string & getProgramName()
Get name of program.
Definition: CmdLine.h:235
std::string & getCopyright()
Get copyright notice.
Definition: CmdLine.h:263
std::string & getLicense()
Get license information.
Definition: CmdLine.h:270
void print_version() const
Print version information.
Definition: CmdLine.cxx:916
virtual ~CmdLine()
Destructor.
Definition: CmdLine.h:148