iRoCS Toolbox  1.1.0
CmdArg.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2005-2015 Olaf Ronneberger, Jörg Mechnich, Florian Pigorsch,
4  * Mario Emmenlauer, Thorsten Schmidt
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: abstract command line argument class
26 ** $RCSfile$
27 ** $Revision: 716 $$Name$
28 ** $Date: 2005-06-09 09:48:17 +0200 (Thu, 09 Jun 2005) $
29 ** Copyright: GPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.11 2005/06/09 07:46:20 tschmidt
38 ** - Changing enum names from bla to CmdLinebla
39 **
40 ** Revision 1.10 2003/01/31 02:52:28 mechnich
41 ** added XML output of all CmdLine arguments
42 **
43 ** Revision 1.9 2002/08/08 10:26:45 pigorsch
44 ** - changed function synopsis so that "[]" are not printed anymore in parameter descriptions
45 **
46 ** Revision 1.8 2002/06/03 15:24:44 pigorsch
47 ** - modified some comments
48 **
49 ** Revision 1.7 2002/05/27 15:35:49 pigorsch
50 ** - removed parameter "detailed" in function synopsis
51 **
52 ** Revision 1.6 2002/05/14 08:15:38 pigorsch
53 ** - added documenation
54 **
55 ** Revision 1.5 2002/04/22 15:56:24 pigorsch
56 ** - added virtual function hasDefaultValue()
57 ** - added virtual function getDefaultValueString()
58 ** - added virtual function clearFixedList()
59 **
60 ** Revision 1.4 2002/04/18 13:54:35 pigorsch
61 ** - made functions fixedSize() and remainingSize() virtual
62 ** - removed parameter fixedSize from constructors
63 ** - removed members pFixedSize, pRemaining
64 ** - renamed function clear(...) clearFlags(...) to avoid ambiguities
65 **
66 ** Revision 1.3 2002/04/10 09:53:56 pigorsch
67 ** - added functions to check if the argument was specified on the
68 ** command line and if it was modified during last parsing
69 **
70 ** Revision 1.2 2002/04/04 12:02:19 pigorsch
71 ** - removed "dummy" stuff
72 **
73 ** Revision 1.1 2002/03/26 07:36:28 ronneber
74 ** restructuring for autoconf
75 **
76 ** Revision 1.1.1.1 2002/03/22 13:45:07 pigorsch
77 ** moved from polsoft repository
78 **
79 **
80 **
81 **************************************************************************/
82 
83 #ifndef CMDARG_HH
84 #define CMDARG_HH
85 
86 #ifdef HAVE_CONFIG_H
87 #include <config.hh>
88 #endif
89 
90 #include <sstream>
91 #include <string>
92 
93 class CmdLine;
94 
95 /*======================================================================*/
101 /*======================================================================*/
102 class CmdArg
103 {
104 public:
105  friend class CmdLine;
106 
107  /*====================================================================*/
111  /*====================================================================*/
112  enum Syntax
113  {
114  isOPT = 0x000,
115  isREQ = 0x001,
116  isVALOPT = 0x002,
117  isVALREQ = 0x004,
118  isVALSEP = 0x008,
119  isVALSTICKY = 0x010,
120  isLIST = 0x020,
121  isPOS = 0x040,
122  isHIDDEN = 0x080,
123  isFIXEDLIST = 0x100,
129  };
130 
131  /*====================================================================*/
135  /*====================================================================*/
137  {
138  GIVEN = 0x01,
139  VALGIVEN = 0x02,
140  OPTION = 0x04,
141  KEYWORD = 0x08,
142  POSITIONAL = 0x10,
143  VALSEP = 0x20,
144  ALLFLAGS = 0x3F
145  };
146 
147  /*====================================================================*/
165  /*====================================================================*/
166  CmdArg(
167  char shortName,
168  const std::string longName,
169  const std::string valueName,
170  const std::string description,
171  CmdArg::Syntax syntax_flags=isOPTVALREQ);
172 
173  /*====================================================================*/
189  /*====================================================================*/
190  CmdArg(
191  char shortName,
192  const std::string longName,
193  const std::string description,
194  CmdArg::Syntax syntax_flags=isOPT);
195 
196  /*====================================================================*/
208  /*====================================================================*/
209  CmdArg(
210  const std::string value,
211  const std::string description,
213 
214  /*====================================================================*/
218  /*====================================================================*/
219  virtual
220  ~CmdArg();
221 
222  /*====================================================================*/
229  /*====================================================================*/
230  virtual
231  unsigned int
232  fixedSize() const;
233 
234  /*====================================================================*/
241  /*====================================================================*/
242  virtual
243  unsigned int
244  remainingSize() const;
245 
246  /*====================================================================*/
262  /*====================================================================*/
263  virtual bool
264  parse(
265  const char* arg,
266  const char*& endptr,
267  CmdLine& cmd)=0;
268 
269  /*====================================================================*/
273  /*====================================================================*/
275  syntax() const;
276 
277  /*====================================================================*/
285  /*====================================================================*/
286  bool
287  hasSyntax(CmdArg::Syntax s) const;
288 
289  /*====================================================================*/
293  /*====================================================================*/
295  flags() const;
296 
297  /*====================================================================*/
305  /*====================================================================*/
306  bool
307  hasFlag(CmdArg::CmdLineFlags f) const;
308 
309  /*====================================================================*/
320  /*====================================================================*/
321  unsigned int
322  sequence() const;
323 
324  /*====================================================================*/
330  /*====================================================================*/
331  char
332  shortName() const;
333 
334  /*====================================================================*/
340  /*====================================================================*/
341  const std::string
342  longName() const;
343 
344  /*====================================================================*/
350  /*====================================================================*/
351  const std::string
352  valueName() const;
353 
354  /*====================================================================*/
360  /*====================================================================*/
361  virtual std::string
362  valueTypeAsString() const=0;
363 
364  /*====================================================================*/
370  /*====================================================================*/
371  virtual std::string
372  valueAsString() const=0;
373 
374  /*====================================================================*/
380  /*====================================================================*/
381  const std::string
382  description() const;
383 
384  /*====================================================================*/
393  /*====================================================================*/
394  const std::string
395  synopsis(
396  bool displayShort=true,
397  bool displayLong=true) const;
398 
399  /*====================================================================*/
405  /*====================================================================*/
406  const std::string
407  errorMsg() const;
408 
409  /*====================================================================*/
413  /*====================================================================*/
414  void
415  clearErrorMsg();
416 
417  /*====================================================================*/
423  /*====================================================================*/
424  void
425  setErrorMsg(const std::string& msg);
426 
427  /*====================================================================*/
433  /*====================================================================*/
434  bool
435  modified() const;
436 
437  /*====================================================================*/
443  /*====================================================================*/
444  void
445  modified(bool m);
446 
447  /*====================================================================*/
453  /*====================================================================*/
454  bool
455  given() const;
456 
457  /*====================================================================*/
463  /*====================================================================*/
464  virtual
465  bool
466  hasDefaultValue() const;
467 
468  /*====================================================================*/
474  /*====================================================================*/
475  virtual
476  std::string
477  getDefaultValueString() const;
478 
479  /*====================================================================*/
483  /*====================================================================*/
484  virtual
485  void
486  clearFixedList();
487 
488 protected:
489  /*====================================================================*/
495  /*====================================================================*/
496  void
497  adjustSyntax();
498 
499  /*====================================================================*/
505  /*====================================================================*/
506  void
508 
509  /*====================================================================*/
515  /*====================================================================*/
516  void
518 
519  /*====================================================================*/
525  /*====================================================================*/
526  void
528 
529  /*====================================================================*/
535  /*====================================================================*/
536  void
537  sequence(unsigned int num);
538 
539 
540 protected:
543 
544  unsigned int pSequence;
546  std::string pLongName;
547  std::string pValueName;
548  std::string pDescription;
549 
550  std::string pErrorMessage;
551 
552  bool pModified;
553 };
554 
555 #include "CmdArg.icc"
556 
557 #endif
CmdArg::Syntax syntax() const
Retrieve the syntax flags for this argument.
virtual bool parse(const char *arg, const char *&endptr, CmdLine &cmd)=0
Parse a value string.
argument was given
Definition: CmdArg.hh:138
virtual std::string valueAsString() const =0
Returns the argument&#39;s value.
virtual ~CmdArg()
Destructor.
char pShortName
Definition: CmdArg.hh:545
argument value must be in the same token
Definition: CmdArg.hh:119
item was matched as a keyword
Definition: CmdArg.hh:141
value was in a separate token
Definition: CmdArg.hh:143
argument value was given
Definition: CmdArg.hh:139
virtual std::string getDefaultValueString() const
Returns the default value as a string.
void setFlags(CmdArg::CmdLineFlags f)
Toggles specified flags.
item was matched as an option
Definition: CmdArg.hh:140
Syntax
Flags that define the argument syntax.
Definition: CmdArg.hh:112
std::string pDescription
Definition: CmdArg.hh:548
bool modified() const
Returns true if the argument was parsed at the last parsing run.
The CmdArg class is the abstract base class for all command line arguments.
Definition: CmdArg.hh:102
virtual unsigned int fixedSize() const
Return fixed size if this CmdArg is a fixed list, return 0 otherwise.
CmdArg::CmdLineFlags pFlags
Definition: CmdArg.hh:541
argument is not to be printed in usage
Definition: CmdArg.hh:122
bool hasSyntax(CmdArg::Syntax s) const
Returns true if the argument&#39;s syntax flags contain s.
const std::string description() const
Get the description (help-message) of this argument.
void adjustSyntax()
Adjust and correct specified syntax flags.
bool hasFlag(CmdArg::CmdLineFlags f) const
Returns true if the argument&#39;s flags contain f.
virtual bool hasDefaultValue() const
Returns true if a default value was specified.
argument value is a list of fixed size
Definition: CmdArg.hh:123
item was matched as a positional argument
Definition: CmdArg.hh:142
const std::string synopsis(bool displayShort=true, bool displayLong=true) const
Generates and returns the arguments synopsis.
The CmdLine class is a parser for command line arguments.
Definition: CmdLine.hh:137
CmdArg::CmdLineFlags flags() const
Retrieve the flags that say how this argument was specified.
argument takes a value
Definition: CmdArg.hh:124
CmdLineFlags
Flags that say how the argument was specied on the command-line.
Definition: CmdArg.hh:136
argument value is required
Definition: CmdArg.hh:117
argument value is optional
Definition: CmdArg.hh:116
CmdArg(char shortName, const std::string longName, const std::string valueName, const std::string description, CmdArg::Syntax syntax_flags=isOPTVALREQ)
Create an argument that takes a value.
argument is positional
Definition: CmdArg.hh:121
bool given() const
Returns true if the argument was parsed in any of the parsing runs.
std::string pErrorMessage
Definition: CmdArg.hh:550
CmdArg::Syntax pSyntax
Definition: CmdArg.hh:542
unsigned int sequence() const
Get the sequence number corresponding to the last time this argument was matched on the command-line...
argument is a list
Definition: CmdArg.hh:120
const std::string errorMsg() const
Returns error message of last parsing.
virtual unsigned int remainingSize() const
Return number of remaining values if this CmdArg is a fixed list, return 0 otherwise.
argument value must be in a separate token
Definition: CmdArg.hh:118
char shortName() const
Returns the argument&#39;s short name.
void clearFlags(CmdArg::CmdLineFlags f=ALLFLAGS)
Clears the specified flags.
std::string pLongName
Definition: CmdArg.hh:546
bool pModified
Definition: CmdArg.hh:552
std::string pValueName
Definition: CmdArg.hh:547
void setErrorMsg(const std::string &msg)
Sets the error message.
virtual void clearFixedList()
If the argument is a fixed list, this function clears all its values.
unsigned int pSequence
Definition: CmdArg.hh:544
argument is required
Definition: CmdArg.hh:115
const std::string valueName() const
Returns the argument&#39;s value name.
const std::string longName() const
Returns the argument&#39;s long name.
void clearErrorMsg()
Clears the error message.
virtual std::string valueTypeAsString() const =0
Returns the argument&#39;s value type.
argument is optional
Definition: CmdArg.hh:114