/usr/include/sofa/helper/ArgumentParser.h is in libsofa1-dev 1.0~beta4-12.
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 | /******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library 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 Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Framework *
* *
* 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.
// Francois Faure, iMAGIS-GRAVIR, May 2001
//========================================================
#ifndef SOFA_HELPER_ARGUMENTPARSER_H
#define SOFA_HELPER_ARGUMENTPARSER_H
#include <sofa/helper/helper.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <list>
#include <vector>
namespace sofa
{
namespace helper
{
typedef std::istringstream istrstream;
/// Abstract base class for all command line arguments
class SOFA_HELPER_API ArgumentBase
{
public:
/// 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);
/// 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;
/// print short name, long name, help
void print () const;
char shortName; ///< Short name
string longName; ///< Long name
string help; ///< Help message
/// 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* >
class Argument : public ArgumentBase {
public:
/** 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 reading lists.
Lists are used for options that can be repeted
Example: run -D ELEM1 -D ELEM2 ...
*/
/*
template<class TE> inline
bool Argument< std::vector<TE> >::read( std::list<std::string>& str)
{
if (str.empty()) return false;
std::string s = str.front();
str.pop_front();
TE val;
istrstream istr( s.c_str() );
if( ! (istr >> val) ) return false;
else {
isSet = true;
*ptr.push_back(vak);
return true;
}
}
*/
template<> inline
bool Argument< std::vector< std::string > >::read( std::list<std::string>& str)
{
if (str.empty()) return false;
std::string s = str.front();
str.pop_front();
isSet = true;
ptr->push_back(s);
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 << " ";
}
/// General case for printing default value
template<> inline
void Argument<std::vector<std::string > >::printValue() const {
for (unsigned int i=0;i<ptr->size();i++)
std::cout << (*ptr)[i] << " ";
}
//========================================================================
/** 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 SOFA_HELPER_API 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;
/// Set of remaining file
std::vector<std::string>* files;
// 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" );
/// Constructor using a global help string and a list of filenames
ArgumentParser( std::vector<std::string>* files, const string& helpstr="", char hlpShrt='h', const string& hlpLng="help" );
/// Constructor using a global help string
~ArgumentParser();
/** 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, const char* lon, const 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, const char* lon, const 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
*/
void operator () ( int argc, char** argv );
void operator () ( std::list<std::string> str );
};
/** 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);
}
inline ArgumentParser parse( std::vector<std::string>* files, const std::string& helpstr="", char hs='h', const std::string& hl="help" ){
return ArgumentParser(files,helpstr,hs,hl);
}
} // namespace helper
} // namespace sofa
#endif
|