gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Minor doc improvements
0 1 0
default
1 file changed with 3 insertions and 3 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -76,97 +76,97 @@
76 76
		  has_syn(false), syn(false), self_delete(false) {}
77 77
    };
78 78

	
79 79
    typedef std::map<std::string,ParData> Opts;
80 80
    Opts _opts;
81 81

	
82 82
    class GroupData 
83 83
    {
84 84
    public:
85 85
      typedef std::list<std::string> Opts;
86 86
      Opts opts;
87 87
      bool only_one;
88 88
      bool mandatory;
89 89
      GroupData() :only_one(false), mandatory(false) {}
90 90
    };
91 91
      
92 92
    typedef std::map<std::string,GroupData> Groups;
93 93
    Groups _groups;
94 94

	
95 95
    struct OtherArg
96 96
    {
97 97
      std::string name;
98 98
      std::string help;
99 99
      OtherArg(std::string n, std::string h) :name(n), help(h) {}
100 100

	
101 101
    };
102 102
      
103 103
    std::vector<OtherArg> _others_help;
104 104
    std::vector<std::string> _file_args;
105 105
    std::string _command_name;
106 106
    
107 107
  public:
108 108

	
109 109
    ///\e
110 110
    ArgParser(int argc, const char **argv);
111 111

	
112 112
    ~ArgParser();
113 113

	
114 114
    ///Add a new integer type option
115 115

	
116 116
    ///\param name The name of the option. The leading '-' must be omitted.
117 117
    ///\param help A help string.
118 118
    ///\retval value The value of the argument will be written to this variable.
119 119
    ///\param obl Indicate if the option is mandatory.
120 120
    ArgParser &intOption(const std::string &name,
121 121
		    const std::string &help,
122 122
		    int value=0, bool obl=false);
123 123

	
124
    ///Add a new floating type option
124
    ///Add a new floating point type option
125 125

	
126 126
    ///\param name The name of the option. The leading '-' must be omitted.
127 127
    ///\param help A help string.
128 128
    ///\retval value The value of the argument will be written to this variable.
129 129
    ///\param obl Indicate if the option is mandatory.
130 130
    ArgParser &doubleOption(const std::string &name,
131 131
		      const std::string &help,
132 132
		      double value=0, bool obl=false);
133 133

	
134 134
    ///Add a new bool type option
135 135

	
136 136
    ///\param name The name of the option. The leading '-' must be omitted.
137 137
    ///\param help A help string.
138 138
    ///\retval value The value of the argument will be written to this variable.
139 139
    ///\param obl Indicate if the option is mandatory.
140 140
    ////\note A mandatory bool obtion is of very little use.)
141 141
    ArgParser &boolOption(const std::string &name,
142 142
		      const std::string &help,
143 143
		      bool value=false, bool obl=false);
144 144

	
145 145
    ///Add a new string type option
146 146

	
147 147
    ///\param name The name of the option. The leading '-' must be omitted.
148 148
    ///\param help A help string.
149 149
    ///\retval value The value of the argument will be written to this variable.
150 150
    ///\param obl Indicate if the option is mandatory.
151 151
    ArgParser &stringOption(const std::string &name,
152 152
		      const std::string &help,
153 153
		      std::string value="", bool obl=false);
154 154
    
155 155
    ///Bind a function to an option.
156 156

	
157 157
    ///\param name The name of the option. The leading '-' must be omitted.
158 158
    ///\param help A help string.
159 159
    ///\retval func The function to be called when the option is given. It
160 160
    ///  must be of type "void f(void *)"
161 161
    ///\param data Data to be passed to \c func
162 162
    ArgParser &funcOption(const std::string &name,
163 163
		    const std::string &help,
164 164
		    void (*func)(void *),void *data);
165 165

	
166 166
    ///\name Options with an external strorage.
167 167
    ///Using this functions, the value of the option will be directly written
168 168
    ///into a variable once the option appears in the command line.
169 169

	
170 170
    ///@{
171 171

	
172 172
    ///Add a new integer type option with a storage reference
... ...
@@ -251,98 +251,98 @@
251 251

	
252 252
    ///With this function you can give help string for non-parsed arguments.
253 253
    ///the parameter \c name will be printed in the short usage line, while
254 254
    ///\c help gives a more detailed description.
255 255
    ArgParser &other(const std::string &name,
256 256
		     const std::string &help="");
257 257
    
258 258
    ///Non option type arguments.
259 259

	
260 260
    ///Gives back a reference to a vector consisting of the program arguments
261 261
    ///not starting with a '-' character.
262 262
    std::vector<std::string> &files() { return _file_args; }
263 263

	
264 264
    ///Give back the command name (the 0th argument)
265 265
    const std::string &commandName() { return _command_name; }
266 266

	
267 267
    void show(std::ostream &os,Opts::iterator i);
268 268
    void show(std::ostream &os,Groups::iterator i);
269 269
    void showHelp(Opts::iterator i);
270 270
    void showHelp(std::vector<OtherArg>::iterator i);
271 271
    void shortHelp();
272 272
    void showHelp();
273 273

	
274 274
    void unknownOpt(std::string arg);
275 275

	
276 276
    void requiresValue(std::string arg, OptType t);
277 277
    void checkMandatories();
278 278
    
279 279
    ///Start the parsing process
280 280
    ArgParser &parse();
281 281

	
282 282
    /// Synonym for parse()
283 283
    ArgParser &run() 
284 284
    {
285 285
      return parse();
286 286
    }
287 287
    
288 288
    ///Check if an opion has been given to the command.
289 289
    bool given(std::string op) 
290 290
    {
291 291
      Opts::iterator i = _opts.find(op);
292 292
      return i!=_opts.end()?i->second.set:false;
293 293
    }
294 294

	
295 295

	
296 296
    ///Magic type for operator[]
297 297
    
298 298
    ///This is the type of the return value of ArgParser::operator[]().
299
    ///It automatically converts to int, double, bool or std::string, if it
300
    ///match the type of the option, otherwise it throws an exception.
299
    ///It automatically converts to int, double, bool or std::string if
300
    ///the type of the option matches, otherwise it throws an exception.
301 301
    ///(i.e. it performs runtime type checking).
302 302
    class RefType 
303 303
    {
304 304
      ArgParser &_parser;
305 305
      std::string _name;
306 306
    public:
307 307
      ///\e
308 308
      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
309 309
      ///\e
310 310
      operator bool() 
311 311
      {
312 312
	Opts::iterator i = _parser._opts.find(_name);
313 313
	LEMON_ASSERT(i==_parser._opts.end(),
314 314
		     std::string()+"Unkown option: '"+_name+"'");
315 315
	LEMON_ASSERT(i->second.type!=ArgParser::BOOL,
316 316
		     std::string()+"'"+_name+"' is a bool option");
317 317
	return *(i->second.bool_p);
318 318
      }
319 319
      ///\e
320 320
      operator std::string()
321 321
      {
322 322
	Opts::iterator i = _parser._opts.find(_name);
323 323
	LEMON_ASSERT(i==_parser._opts.end(),
324 324
		     std::string()+"Unkown option: '"+_name+"'");
325 325
	LEMON_ASSERT(i->second.type!=ArgParser::STRING,
326 326
		     std::string()+"'"+_name+"' is a string option");
327 327
	return *(i->second.string_p);
328 328
      }
329 329
      ///\e
330 330
      operator double() 
331 331
      {
332 332
	Opts::iterator i = _parser._opts.find(_name);
333 333
	LEMON_ASSERT(i==_parser._opts.end(),
334 334
		     std::string()+"Unkown option: '"+_name+"'");
335 335
	LEMON_ASSERT(i->second.type!=ArgParser::DOUBLE &&
336 336
		     i->second.type!=ArgParser::INTEGER,
337 337
		     std::string()+"'"+_name+"' is a floating point option");
338 338
	return i->second.type==ArgParser::DOUBLE ?
339 339
	  *(i->second.double_p) : *(i->second.int_p);
340 340
      }
341 341
      ///\e
342 342
      operator int() 
343 343
      {
344 344
	Opts::iterator i = _parser._opts.find(_name);
345 345
	LEMON_ASSERT(i==_parser._opts.end(),
346 346
		     std::string()+"Unkown option: '"+_name+"'");
347 347
	LEMON_ASSERT(i->second.type!=ArgParser::INTEGER,
348 348
		     std::string()+"'"+_name+"' is an integer option");
0 comments (0 inline)