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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/**
* @file: conf.h
* Implementation of options configuration of ShowGraph
* @defgroup Opts Options
* @brief Classes for configuring and parsing command-line options
* @ingroup Utils
*/
/*
* Utils library in Showgraph tool
* Copyright (c) 2009, Boris Shurygin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CONF_H
#define CONF_H
#include <QString>
#include <QHash>
/**
* Command line option type
* @ingroup Opts
*/
enum OptType
{
/** Boolean */
OPT_BOOL,
/** Integer */
OPT_INT,
/** Float */
OPT_FLOAT,
/** String */
OPT_STRING,
/** Number of types */
OPT_TYPES_NUM
};
/**
* Union type for option's values
* @ingroup Opts
*/
union OptValues
{
/** Value for boolean option */
bool bool_val;
/** Value for integer option */
int int_val;
/** Value for floating point option */
qreal float_val;
};
/**
* Command line option descrition
* @ingroup Opts
*/
class Option
{
/** Whether user has set value of this option */
bool defined;
/** Type */
OptType _type;
/** Option default values */
OptValues def_values;
/** Option user's values */
OptValues values;
QString string_val;
/** Short name */
QString short_name;
/** Long name */
QString long_name;
/** Description */
QString descr;
public:
/** Constructor without default val */
Option( OptType _t, QString sname, QString lname, QString d):
defined( false),_type( _t), short_name( sname), long_name( lname), descr( d)
{
switch ( _t)
{
case OPT_BOOL:
def_values.bool_val = false;
break;
case OPT_INT:
def_values.int_val = 0;
break;
case OPT_FLOAT:
def_values.float_val = 0;
break;
case OPT_STRING:
default:
break;
}
values = def_values;
};
/** Constructor with default bool val */
Option( QString sname, QString lname, QString d, bool val):
defined( false), _type( OPT_BOOL), short_name( sname), long_name( lname), descr( d)
{
def_values.bool_val = val;
values = def_values;
}
/** Constructor for string option */
Option( QString sname, QString lname, QString d):
defined( false), _type( OPT_STRING), short_name( sname), long_name( lname), descr( d){};
/** Check if the option was defined */
inline bool isDefined() const
{
return defined;
}
/** Set the option to 'defined' state*/
inline void setDefined( bool def = true)
{
defined = def;
}
/** Get short name */
inline QString shortName() const
{
return short_name;
}
/** Get long name */
inline QString longName() const
{
return long_name;
}
/** Get option type */
inline OptType type() const
{
return _type;
}
/** Get option default val */
inline bool defBoolVal() const
{
assertd( _type == OPT_BOOL);
return def_values.bool_val;
}
/** Set option boolean value */
inline void setBoolVal( bool val)
{
assertd( _type == OPT_BOOL);
values.bool_val = val;
}
/** Set option boolean value */
inline void setIntVal( int val)
{
assertd( _type == OPT_INT);
values.int_val = val;
}
/** Set option boolean value */
inline void setFloatVal( qreal val)
{
assertd( _type == OPT_FLOAT);
values.float_val = val;
}
/** Set option boolean value */
inline void setStringVal( QString val)
{
assertd( _type == OPT_STRING);
string_val = val;
}
/** Get string value of option */
inline QString string() const
{
assertd( _type == OPT_STRING);
return string_val;
}
/** Get int value of option */
inline int intVal() const
{
assertd( _type == OPT_INT);
return values.int_val;
}
/** Get float value of option */
inline qreal floatVal() const
{
assertd( _type == OPT_FLOAT);
return values.float_val;
}
/** Get int value of option */
inline int isSet() const
{
assertd( _type == OPT_BOOL);
return values.bool_val;
}
/** Print option's synopsis and description */
void print( QTextStream &stream);
};
/**
* @brief Configuration represention
* @ingroup Opts
*
* @par
* Allows for configuring command line options and reading them from C-string array.
* Usage example:
@code
// simple main routine
int main( int argc, char **argv)
{
Conf conf;
// Configuring possible options and their type
conf.addOption( new Option( OPT_STRING, "f", "file", "input graph description file name"));
conf.addOption( new Option( OPT_STRING, "o", "output", "output image file name"));
conf.readArgs( argc, argv);
//checking options and reading their values
if ( conf.longOption("file")->isDefined()
&& conf.longOption("output")->isDefined())
{
QString xmlname = conf.longOption("file")->string(); // read value passed with '-file ...'
...
} else
{
conf.printOpts(); // Print options to console
}
}
@endcode
*/
class Conf
{
QString app_name;
QHash< QString, Option *> short_opts;
QHash< QString, Option *> long_opts;
QList< QString> unknown_options;
public:
/** Constructor */
Conf();
/** Destructor wipes all the options */
~Conf()
{
foreach( Option *opt, short_opts)
{
delete opt;
}
}
/** Get number of arguments that were not recognized */
inline int unknownOptsNum() const
{
return unknown_options.count();
}
/** Add option */
inline void addOption( Option *opt)
{
short_opts[ opt->shortName()] = opt;
long_opts[ opt->longName()] = opt;
}
/** Convenience routine: adds option without default val */
inline void addOption( OptType _t, QString sname, QString lname, QString d)
{
Option *opt = new Option( _t, sname, lname, d);
addOption( opt);
}
/** Convenience routine: adds option with default bool val */
inline void addOption( QString sname, QString lname, QString d, bool val)
{
Option *opt = new Option( sname, lname, d, val);
addOption( opt);
}
/** Convenience routine: adds string option without default value */
inline void addOption( QString sname, QString lname, QString d)
{
Option *opt = new Option( sname, lname, d);
addOption( opt);
}
/** Print options */
void printOpts();
/** Print value defaults */
void printDefaults();
/**
* Parse arguments from C-string
* @param argc number of arguments
* @param argv pointer to array of C-strings
*/
void readArgs( int argc, char** argv);
/** Get option based on its name */
inline Option* option( QString name)
{
/* try to look among short options */
if ( short_opts.find( name) != short_opts.end())
{
return short_opts[ name];
}
/* try to look among long options */
if ( long_opts.find( name) != long_opts.end())
{
return long_opts[ name];
}
/* if nothing's found return NULL */
return NULL;
}
/** Get option based on its short name */
inline Option* shortOption( QString name)
{
/* try to look among short options */
if ( short_opts.find( name) != short_opts.end())
{
return short_opts[ name];
}
/* if nothing's found return NULL */
return NULL;
}
/** Get option based on its long name */
inline Option* longOption( QString name)
{
/* try to look among long options */
if ( long_opts.find( name) != long_opts.end())
{
return long_opts[ name];
}
/* if nothing's found return NULL */
return NULL;
}
};
#endif
|