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
|
/******************************************************************************
* 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
//========================================================
#include "ArgumentParser.h"
namespace sofa
{
namespace helper
{
typedef std::istringstream istrstream;
ArgumentBase::ArgumentBase(char s, string l, string h, bool m)
: shortName(s)
, longName(l)
, help(h)
, mandatory(m)
, isSet(false)
{}
/// Base destructor: does nothing.
ArgumentBase::~ArgumentBase()
{}
/// print short name, long name, help
void ArgumentBase::print () const
{
std::cout << "-" << shortName <<",\t--"<< longName <<":\t" << help;
if( mandatory ) std::cout<< " (required) ";
std::cout << " (default: "; printValue();
std::cout << ")" << std::endl;
}
//========================================================================
/// Constructor using a global help string
ArgumentParser::ArgumentParser( const string& helpstr, char hlpShrt, const string& hlpLng )
: files(NULL)
, globalHelp( helpstr )
, helpShortName(hlpShrt)
, helpLongName(hlpLng)
{}
/// Constructor using a global help string and a list of filenames
ArgumentParser::ArgumentParser( std::vector<std::string>* files, const string& helpstr, char hlpShrt, const string& hlpLng )
: files(files)
, globalHelp( helpstr )
, helpShortName(hlpShrt)
, helpLongName(hlpLng)
{}
/// Constructor using a global help string
ArgumentParser::~ArgumentParser()
{
for( ArgVec::const_iterator a=commands.begin(), aend=commands.end(); a!=aend; ++a )
delete (*a);
}
/** Parse a command line
\param argc number of arguments + 1, as usual in C
\param argv arguments
*/
void ArgumentParser::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);
}
void ArgumentParser::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();
if( files )
std::cout << "others: file names" << std::endl;
exit(1);
}
// not an option
else if( files && name[0]!='-' ){
files->push_back(name);
}
// indicating the next argument is not an option
else if( files && name=="--" ){
files->push_back(str.front());
str.pop_front();
}
// 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);
}
} // namespace helper
} // namespace sofa
|