/usr/include/projects/SensAble/argumentParser.h is in libsofa1-dev 1.0~beta4-11+b3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | /******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This program 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 2 of the License, or (at your option) *
* any later version. *
* *
* This program 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 this program; if not, write to the Free Software Foundation, Inc., 51 *
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
*******************************************************************************
* SOFA :: Applications *
* *
* Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
* H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza, *
* M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
//========================================================
// Yet another command line parser.
// Fran�is Faure, iMAGIS-GRAVIR, May 2001
//========================================================
#ifndef ANIMAL_command_line_parser________
#define ANIMAL_command_line_parser________
#include <iostream>
#include <stdlib.h>
//#include <strstream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <list>
typedef std::istringstream istrstream;
/// Abstract base class for all command line arguments
struct ArgumentBase
{
/// character string
typedef std::string string;
/** Constructor
\param s short name
\param l long name
\param h help
\param m true iff the argument is mandatory
*/
ArgumentBase(char s, string l, string h, bool m)
: shortName(s)
, longName(l)
, help(h)
, mandatory(m)
, isSet(false)
{}
/// Base destructor: does nothing.
virtual ~ArgumentBase(){}
/// Read the command line
virtual bool read( std::list<std::string>& str ) = 0;
/// Print the value of the associated variable
virtual void printValue() const =0;
char shortName; ///< Short name
string longName; ///< Long name
string help; ///< Help message
/// print short name, long name, help
inline void print () const
{
std::cout << "-" << shortName <<",\t--"<< longName <<":\t" << help;
if( mandatory ) std::cout<< " (required) ";
std::cout << " (default: "; printValue();
std::cout << ")" << std::endl;
}
/// True iff the value must be set
bool mandatory;
/// True iff a value has bee read on the command line
bool isSet;
};
//=========================================================================
/** Command line argument.
\brief Contains a pointer to a value which can be parsed by a ArgumentParser.
Contains also a short name, a long name and a help message.
@see ArgumentParser
*/
template < class T = void* >
struct Argument : public ArgumentBase {
/** Constructor
\param t a pointer to the value
\param sn short name of the argument
\param ln long name of the argument
\param h help on the argument
\param m true iff the argument is mandatory
*/
Argument( T* t, char sn, string ln, string h, bool m )
: ArgumentBase(sn,ln,h,m)
, ptr(t)
{}
inline void printValue() const ;
private:
/// Pointer to the parameter
T* ptr;
/** Try to read argument value from an input stream.
Return false if failed
*/
inline bool read( std::list<std::string>& str ) {
if (str.empty()) return false;
std::string s = str.front();
str.pop_front();
istrstream istr( s.c_str() );
if( ! (istr >> *ptr) ) return false;
else {
isSet = true;
return true;
}
}
};
/** Specialization for flag reading booleans.
Booleans are seen as flags that you can set to TRUE using the command line.
Example: run --verbose
The advantage is that you do not have to set the value, it is automatically TRUE.
The drawback is that reading a boolean necessarily sets it to TRUE. Currently you can not set a boolean to FALSE using this parser.
*/
template<> inline
bool Argument<bool>::read( std::list<std::string>& )
{
*ptr = true;
isSet = true;
return true;
}
template<> inline
bool Argument<std::string>::read( std::list<std::string>& str )
{
if (str.empty()) return false;
std::string s = str.front();
str.pop_front();
*ptr = s;
isSet = true;
return true;
}
/// General case for printing default value
template<class T> inline
void Argument<T>::printValue() const {
std::cout << *ptr << " ";
}
//========================================================================
/** Command line parser
This object parses arguments from a command line or from an input stream.
The arguments are described using a pointer, a short name, a long name and a help message. Mandatory arguments are declared using method "parameter", optional arguments are declared using method "option".
Once all arguments declared, operator () does the parsing.
The special option -h or --help displays help on all arguments.
See examples argumentParserLine_test.cpp and argumentParserFile_test.cpp
@see Argument
*/
class ArgumentParser {
/// String
typedef std::string string;
/// Associate a string with a Argument object
typedef std::map< string, ArgumentBase* > Map;
/// short name -> Argument object
std::map< char, ArgumentBase* > shortName;
/// long name -> Argument object
Map longName;
/// Associate name with boolean value (true iff it is set)
typedef std::map<ArgumentBase*,bool> SetMap;
/// Set map (bool true iff parameter is set)
SetMap parameter_set;
/// Set of commands
typedef std::vector<ArgumentBase*> ArgVec;
/// Set of commands
ArgVec commands;
// help stuff
string globalHelp; ///< Overall presentation
char helpShortName; ///< short name for help
string helpLongName; ///< long name for help
public:
/// Constructor using a global help string
ArgumentParser( const string& helpstr="", char hlpShrt='h', const string& hlpLng="help" )
: globalHelp( helpstr )
, helpShortName(hlpShrt)
, helpLongName(hlpLng)
{}
/** Declare an optional argument
\param ptr pointer to the variable
\param sho short name
\param lon long name
\param help
*/
template<class T> inline
ArgumentParser& option( T* ptr, char sho, char* lon, char* help )
{
string sn, ln(lon), h(help); sn += sho;
if( sho!=0 && shortName.find(sho) != shortName.end() ){
std::cerr << "name " << sn << " already used !" << std::endl;
exit( 1 );
}
if( ln.size()>0 && longName.find(ln) != longName.end() ){
std::cerr << "name " << ln << " already used !" << std::endl;
exit( 1 );
}
if( sho!=0 && sho == helpShortName ){
std::cerr << "name " << sho << " reserved for help !" << std::endl;
exit( 1 );
}
if( ln.size()>0 && lon == helpLongName ){
std::cerr << "name " << lon << " reserved for help !" << std::endl;
exit( 1 );
}
ArgumentBase* c = new Argument<T>(ptr,sho,ln,h,false);
shortName[sho] = c;
longName[lon] = c;
commands.push_back(c);
return (*this);
}
/** Declare a mandatory argument
\param ptr pointer to the variable
\param sho short name
\param lon long name
\param help
*/
template<class T> inline
ArgumentParser& parameter( T* ptr, char sho, char* lon, char* help )
{
string sn, ln(lon), h(help); sn += sho;
if( sho!=0 && shortName.find(sho) != shortName.end() ){
std::cerr << "name " << sn << " already used !" << std::endl;
exit( 1 );
}
if( ln.size()>0 && longName.find(ln) != longName.end() ){
std::cerr << "name " << ln << " already used !" << std::endl;
exit( 1 );
}
if( sho!=0 && sho == helpShortName ){
std::cerr << "name " << sho << " reserved for help !" << std::endl;
exit( 1 );
}
if( ln.size()>0 && lon == helpLongName ){
std::cerr << "name " << lon << " reserved for help !" << std::endl;
exit( 1 );
}
ArgumentBase* c = new Argument<T>(ptr,sho,ln,h,true);
shortName[sho] = c;
longName[lon] = c;
commands.push_back(c);
return (*this);
}
/** Parse a command line
\param argc number of arguments + 1, as usual in C
\param argv arguments
*/
inline void operator () ( int argc, char** argv )
{
std::list<std::string> str;
for (int i=1;i<argc;++i)
str.push_back(std::string(argv[i]));
(*this)(str);
}
inline void operator () ( std::list<std::string> str )
{
string shHelp("-"); shHelp.push_back( helpShortName );
string lgHelp("--"); lgHelp.append( helpLongName );
string name;
while( !str.empty() ){
name = str.front();
str.pop_front();
// std::cout << "name = " << name << std::endl;
// std::cout << "lgHelp = " << lgHelp << std::endl;
// std::cout << "shHelp = " << shHelp << std::endl;
// display help
if( name == shHelp || name == lgHelp )
{
if( globalHelp.size()>0 ) std::cout<< globalHelp <<std::endl;
std::cout << "(short name, long name, description, default value)\n-h,\t--help: this help" << std::endl;
for( ArgVec::const_iterator a=commands.begin(), aend=commands.end(); a!=aend; ++a )
(*a)->print();
exit(1);
}
// long name
else if( name.length() > 1 && name[0]=='-' && name[1]=='-' ){
string a;
for( unsigned int i=2; i<name.length(); ++i ){
a += name[i];
}
if( longName.find(a) != longName.end() ){
if( !(longName[ a ]->read( str )))
std::cerr<< "\ncould not read value for option " << name << std::endl << std::endl;
else parameter_set[longName[ a ]] = true;
}
else std::cerr << "\nUnknown option " << name << std::endl << std::endl;
}
// short names (possibly concatenated)
else if( name.length() > 1 && name[0]=='-' && name[1]!='-' ){
for( unsigned int i=1; i<name.length(); ++i ){
char a = name[i];
if( shortName.find(a) != shortName.end() ){
if( !(shortName[ a ]->read( str )))
std::cerr<< "\ncould not read value for option " << name << std::endl << std::endl;
else parameter_set[shortName[ a ]] = true;
}
else std::cerr << "\nUnknown option " << name[i] << std::endl << std::endl;
}
}
//
else std::cerr << "Unknown option " << name << std::endl;
}
// Unset mandatory arguments ?
bool unset = false;
for( ArgVec::const_iterator cm = commands.begin(), cmend=commands.end(); cm != cmend; ++cm )
{
if( (*cm)->mandatory && !(*cm)->isSet ){
if( !unset )
{
std::cout << "Please set the following parameters: (short name, long name, description)" << std::endl;
unset = true;
}
(*cm)->print();
}
}
if( unset ) exit(1);
}
};
/** Parse a command line
\param helpstr General help message
\param hs short name for help
\param hl long name for help
This method frees the programmer from explicitly creating an ArgumentParser, which makes the program (hopefully) more readable. Using this method, the ArgumentParser is transparently created, it receives and processes the arguments, then it is destroyed.
*/
inline ArgumentParser parse( const std::string& helpstr="", char hs='h', const std::string& hl="help" ){
return ArgumentParser(helpstr,hs,hl);
}
#endif
|