GNU CommonC++
cmdoptns.h
Go to the documentation of this file.
1 // Copyright (C) 2001-2010 Gianni Mariani
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_CMDOPTNS_H_
44 #define CCXX_CMDOPTNS_H_
45 
46 #ifndef CCXX_STRING_H_
47 #include <cc++/string.h>
48 #endif
49 
50 #ifdef CCXX_NAMESPACES
51 namespace ost {
52 #endif
53 
54 class CommandOption;
55 class CommandOptionParse;
56 
64 extern __EXPORT CommandOption * defaultCommandOptionList;
65 
77 public:
78 
83  const char * optionName;
84 
89  const char * optionLetter;
90 
96  const char * description;
97 
103  enum OptionType {
119  collect
120  };
121 
125  OptionType optionType; // HasArg, NoArg or Trailing
126 
131  bool required; // Option is required - fail without it
132 
138 
142  virtual ~CommandOption();
143 
156  const char * inOptionName,
157  const char * inOptionLetter,
158  const char * inDescription,
159  OptionType inOptionType,
160  bool inRequired = false,
162  );
163 
171  virtual void foundOption( CommandOptionParse * cop, const char * value = 0 );
172 
181  virtual void foundOption( CommandOptionParse * cop, const char ** value, int num );
182 
189  virtual void parseDone( CommandOptionParse * cop );
190 
198  virtual void performTask( CommandOptionParse * cop );
199 
206  virtual bool hasValue();
207 
208 };
209 
215 public:
216 
220  const char ** values;
221 
225  int numValue;
226 
239  const char * inOptionName,
240  const char * inOptionLetter,
241  const char * inDescription,
242  OptionType inOptionType,
243  bool inRequired = false,
245  );
246 
248 
249  virtual void foundOption( CommandOptionParse * cop, const char * value = 0 );
250  virtual void foundOption( CommandOptionParse * cop, const char ** value, int num );
251  virtual bool hasValue();
252 };
253 
258 public:
259 
271  const char * inOptionName,
272  const char * inOptionLetter,
273  const char * inDescription,
274  bool inRequired = false,
276  );
277 
278  virtual ~CommandOptionArg();
279 
280 
281 };
282 
293 public:
294 
306  const char * inOptionName,
307  const char * inOptionLetter,
308  const char * inDescription,
309  bool inRequired = false,
311  );
312 
313 };
314 
323 public:
324 
336  const char * inOptionName,
337  const char * inOptionLetter,
338  const char * inDescription,
339  bool inRequired = false,
341  );
342 
343 };
344 
349 public:
350 
354  int numSet; // The number of times this argument is set
355 
367  const char * inOptionName,
368  const char * inOptionLetter,
369  const char * inDescription,
370  bool inRequired = false,
372  );
373 
377  virtual void foundOption( CommandOptionParse * cop, const char * value = 0 );
378 
379 };
380 
391 public:
392 
396  virtual ~CommandOptionParse() = 0;
397 
401  virtual bool argsHaveError() = 0;
402 
406  virtual const char * printErrors() = 0;
407 
411  virtual const char * printUsage() = 0;
412 
417  virtual void registerError( const char * errMsg ) = 0;
418 
423  virtual void performTask() = 0;
424 
425 };
426 
436  int argc,
437  char ** argv,
438  const char * comment,
440 );
441 
442 #ifdef CCXX_NAMESPACES
443 }
444 #endif
445 
446 #endif
447 
ost::CommandOption
CommandOption is the base class for all command line options.
Definition: cmdoptns.h:76
ost::CommandOption::performTask
virtual void performTask(CommandOptionParse *cop)
Once CommandOption objects have completed parsing and there are no errors they may have some specific...
ost::CommandOptionWithArg::foundOption
virtual void foundOption(CommandOptionParse *cop, const char **value, int num)
ost::CommandOptionWithArg::values
const char ** values
Array of list of values collected for this option.
Definition: cmdoptns.h:220
ost::CommandOption::optionName
const char * optionName
Long option name, these will be preceded with "--" on the command line.
Definition: cmdoptns.h:83
ost::CommandOptionWithArg::~CommandOptionWithArg
virtual ~CommandOptionWithArg()
ost::CommandOption::parseDone
virtual void parseDone(CommandOptionParse *cop)
Once parsing of command line options is complete, this method is called.
ost::CommandOption::foundOption
virtual void foundOption(CommandOptionParse *cop, const char *value=0)
foundOption is called by the CommandOptionParse object during the parsing of the command line options...
ost::CommandOptionWithArg::CommandOptionWithArg
CommandOptionWithArg(const char *inOptionName, const char *inOptionLetter, const char *inDescription, OptionType inOptionType, bool inRequired=false, CommandOption **ppNext=&defaultCommandOptionList)
CommandOptionWithArg contructor.
string.h
Common C++ generic string class.
ost::CommandOptionArg::CommandOptionArg
CommandOptionArg(const char *inOptionName, const char *inOptionLetter, const char *inDescription, bool inRequired=false, CommandOption **ppNext=&defaultCommandOptionList)
CommandOptionArg contructor.
ost::CommandOption::OptionType
OptionType
OptionType is for denoting what type of option this is, with an arg, without an arg or the trailing a...
Definition: cmdoptns.h:103
ost::CommandOptionRest::CommandOptionRest
CommandOptionRest(const char *inOptionName, const char *inOptionLetter, const char *inDescription, bool inRequired=false, CommandOption **ppNext=&defaultCommandOptionList)
CommandOptionRest contructor.
ost::CommandOptionParse::registerError
virtual void registerError(const char *errMsg)=0
Register an error with this parser.
ost::CommandOptionNoArg::numSet
int numSet
The number of times this value has been set.
Definition: cmdoptns.h:354
ost::CommandOptionWithArg::numValue
int numValue
Number of values in the values array.
Definition: cmdoptns.h:225
ost::CommandOptionRest
It only makes sense to have a single one of these set and it is exclusive with CommandOptionCollect.
Definition: cmdoptns.h:292
ost::makeCommandOptionParse
__EXPORT CommandOptionParse * makeCommandOptionParse(int argc, char **argv, const char *comment, CommandOption *options=defaultCommandOptionList)
makeCommandOptionParse will create an implementation of a CommandOptionParse object.
ost::CommandOption::hasArg
@ hasArg
This option is associated with a value.
Definition: cmdoptns.h:107
ost::CommandOption::CommandOption
CommandOption(const char *inOptionName, const char *inOptionLetter, const char *inDescription, OptionType inOptionType, bool inRequired=false, CommandOption **ppNext=&defaultCommandOptionList)
CommandOption contructor.
ost::CommandOptionCollect
It only makes sense to have a single one of these set and it is also exclusive with CommandOptionRest...
Definition: cmdoptns.h:322
ost::CommandOptionParse::performTask
virtual void performTask()=0
The method should be invoked by the main code once it has determined that the application should be s...
ost::CommandOptionParse::printUsage
virtual const char * printUsage()=0
Return a string that contains the usage description of this list of paramaters.
ost::CommandOptionNoArg::CommandOptionNoArg
CommandOptionNoArg(const char *inOptionName, const char *inOptionLetter, const char *inDescription, bool inRequired=false, CommandOption **ppNext=&defaultCommandOptionList)
CommandOptionArg contructor.
ost::CommandOption::optionLetter
const char * optionLetter
option letter, these will be preceded with "-" on the command line.
Definition: cmdoptns.h:89
__EXPORT
#define __EXPORT
Definition: config.h:979
ost::CommandOptionParse
This is the CommandOptionParse interface class.
Definition: cmdoptns.h:390
ost::CommandOption::~CommandOption
virtual ~CommandOption()
A virtual destructor just in case.
ost::CommandOptionParse::~CommandOptionParse
virtual ~CommandOptionParse()=0
Virtual destructor needed so that the object may be correctly deleted.
ost::CommandOption::description
const char * description
A short description of the option for Usage messages.
Definition: cmdoptns.h:96
ost::CommandOptionWithArg::hasValue
virtual bool hasValue()
ost::CommandOptionWithArg
Derived class of CommandOption for options that have a value associated with them.
Definition: cmdoptns.h:214
ost::CommandOptionParse::printErrors
virtual const char * printErrors()=0
Return a string of text describing the list of errors encountered.
ost::CommandOptionCollect::CommandOptionCollect
CommandOptionCollect(const char *inOptionName, const char *inOptionLetter, const char *inDescription, bool inRequired=false, CommandOption **ppNext=&defaultCommandOptionList)
CommandOptionRest contructor.
ost::CommandOptionArg::~CommandOptionArg
virtual ~CommandOptionArg()
ost::CommandOption::hasValue
virtual bool hasValue()
For fields with the required flag set, this method is used to determine if the Option has satisfied i...
ost::CommandOption::required
bool required
True if this parameter is required.
Definition: cmdoptns.h:131
ost::CommandOption::noArg
@ noArg
This option is a flag only.
Definition: cmdoptns.h:111
ost
Definition: address.h:64
ost::CommandOptionParse::argsHaveError
virtual bool argsHaveError()=0
Get the value of the error flag set if the parser encountered errors.
ost::CommandOptionNoArg::foundOption
virtual void foundOption(CommandOptionParse *cop, const char *value=0)
CommandOptionNoArg::foundOption will evpect a nil "value" passed in.
ost::CommandOption::foundOption
virtual void foundOption(CommandOptionParse *cop, const char **value, int num)
foundOption is called by the CommandOptionParse object during the parsing of the command line options...
ost::CommandOptionWithArg::foundOption
virtual void foundOption(CommandOptionParse *cop, const char *value=0)
ost::CommandOption::trailing
@ trailing
Remaining of the command line arguments.
Definition: cmdoptns.h:115
ost::CommandOption::next
CommandOption * next
This next CommandOption in this list of options or nil if no more options exist.
Definition: cmdoptns.h:137
ost::CommandOptionArg
Class for options with an argument e.g.
Definition: cmdoptns.h:257
ost::CommandOptionNoArg
CommandOption type for flags.
Definition: cmdoptns.h:348
ost::defaultCommandOptionList
__EXPORT CommandOption * defaultCommandOptionList
This defines a linked list head pointer for all the command line options that use the default list.
ost::CommandOption::optionType
OptionType optionType
This command option's OptionType.
Definition: cmdoptns.h:125