summaryrefslogtreecommitdiffstats
path: root/commline/cmlparser.hh
blob: 6a63c185d36e6088cf86a1507b4a4a85587ace74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * INCLUDE: cmlparser.hh
 *
 * MODULE:  command line interpreter
 * 
 * PURPOSE:
 *   	Tool for interpreting command line arguments
 *
 * COMMENTS:
 *			None
*/

/**
*	@defgroup Commline Commline
*
*	@file cmlparser.hh
*
*	@ingroup Commline
*/

#ifndef AK_CMLPARSER_HH
#define AK_CMLPARSER_HH



#include <list>
#include <string>
#include <exception>
#include <iostream>

using std::string;
using std::list;
using std::ostream;
using std::cout;

// Command Line Parser version
extern const char* CommandLineParserVersion;

// specific errors thrown by the parser
class CmlException : public std::exception
  {
    public:
      explicit CmlException(const string& whatString);
      virtual ~CmlException() throw();
      virtual  const char* what() const throw();
      
    protected:  
      string  problem;
   };


class CommandLineParameter
  {
    public:

     static const char* defaultTitle;
     static const char* descSep;     
     static const char* descTab;
     static const char* descIndent;
     static const char* descLineSep;
     static const char  descOpen;
     static const char  descClose;
     static const char* descLeftDefault;
     static const char* descRightDefault;

     virtual ~CommandLineParameter(); 
     
     //interface for Parser
     void setDescription(const char*);
     
     bool doesMatch(char c);
     bool doesMatch(const char* s);
     
     char         getShortName() const;
     const char*  getLongName() const;
     
     virtual bool setPresent(char c) throw(CmlException) = 0;
     virtual bool setPresent(const char* s) throw(CmlException) = 0;
    
     virtual bool needsValue() = 0;
     virtual bool takeValue(const char* s) = 0;
     virtual void popValue() = 0;
     
     void virtual reset();
     const char *calledName();     
     
     // has a (at least one) value been assigned?
     virtual bool isPresent() = 0;

     virtual const char* getValueAsString() throw(CmlException) = 0;
     virtual long        getValueAsLong()   throw(CmlException) = 0;
     virtual double      getValueAsDouble() throw(CmlException) = 0;

     virtual ostream& printStatus(ostream& = cout) = 0;
     ostream& printHelp(ostream& = cout);

    protected:
      CommandLineParameter(char newShortName, const char* newLongName, const char* newDefaultValue) throw(CmlException);
      CommandLineParameter(char newShortName, const char* newLongName, long newDefaultValue) throw(CmlException);
      
    protected:
    
      char  shortName;
      char* longName;
      bool  present;
      bool  wasLongName;
      
      char* defaultValue;
      char  shNameString[2];
      
      char *descriptionText; 
      char *paramDescription;
   };


class FlagParameter: public CommandLineParameter
  {
   public:
     FlagParameter(char nShortName, const char*nLongName) throw(CmlException);

     bool setPresent(char c) throw(CmlException);
     bool setPresent(const char* s) throw(CmlException);
     bool isPresent();
    
     bool needsValue();
     bool takeValue(const char* s);
     void popValue();
     
     const char* getValueAsString() throw(CmlException);
     long        getValueAsLong()   throw(CmlException);
     double      getValueAsDouble() throw(CmlException);
     
     ostream& printStatus(ostream& = cout);
   };

class StringParameter: public CommandLineParameter
  {
   private:
      list<char*> value;
   
   public:
     StringParameter(char nShortName, const char* nLongName, const char *newDefaultValue = NULL) throw(CmlException);
     StringParameter(char nShortName, const char* nLongName, long newDefaultValue = 0L) throw(CmlException);
     ~StringParameter();
      
     bool setPresent(char c) throw(CmlException);
     bool setPresent(const char* s) throw(CmlException);
     bool isPresent();
    
     bool needsValue();
     bool takeValue(const char* s);
     void popValue();
     
     const char* getValueAsString() throw(CmlException);
     long        getValueAsLong()   throw(CmlException);
     double      getValueAsDouble() throw(CmlException);
     
     void reset();
     
     ostream& printStatus(ostream& = cout);
    
   };


class CommandLineParser
  {
   public:
     static const char noShortName;
     static const char* noLongName;     
     static const char* ShortSign;
     static const char* LongSign;
     
    
     static CommandLineParser& getInstance();
     
     ~CommandLineParser();

	/*
	These functions take a parameter called description. This is a string used in printHelp
	The format of this string has to be:
		<name of parameter> line1\n\t\tline2...\n\t\tlineN
		brackets<> and space after are mandatory if there is a parameter!
		Otherwise no <>!
	*/
     CommandLineParameter& addFlagParameter(char shortName, const char* longName, const char* description) throw(CmlException);
     CommandLineParameter& addStringParameter(char shortName, const char* longName,  const char* description, const char *newDefaultValue = NULL) throw(CmlException);
     CommandLineParameter& addLongParameter(char shortName, const char* longName,  const char* description, long newDefaultValue = 0L ) throw(CmlException);

     bool isPresent(char shortName) throw(CmlException);
     bool isPresent(const char* longName) throw(CmlException);

     const char* getValueAsString(char shortName) throw(CmlException);
     long        getValueAsLong(char shortName)   throw(CmlException);
     double      getValueAsDouble(char shortName) throw(CmlException);
     
     const char* getValueAsString(const char* longName) throw(CmlException);
     long        getValueAsLong(const char* longName)   throw(CmlException);
     double      getValueAsDouble(const char* longName) throw(CmlException);

     void processCommandLine(int argc, char** argv) throw(CmlException);

     bool testProcessCommandLine(const char* test_cml);
     
     void printHelp();
     void printStatus();

   private:
     static CommandLineParser* myself;
      
     list<CommandLineParameter*> cmlParameter;

     CommandLineParameter *lastParameter;
     bool nextTokenIsValue;

     CommandLineParser();

     CommandLineParameter& getParameter(char shortName) throw(CmlException);
     CommandLineParameter& getParameter(const char* longName) throw(CmlException);

     void setValue(const char* value) throw(CmlException);

     void longNameParameter(const char* nextToken) throw(CmlException);

     void shortNameParameter(const char* nextToken) throw(CmlException);
   };
   
#endif