OpendTect-6_4  6.4
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
CommandLineParser Class Reference

Public Member Functions

 CommandLineParser (const char *)
 
 CommandLineParser (int argc, char **argv)
 
 CommandLineParser ()
 
void setKeyHasValue (const char *key, int nrvals=1)
 
void getNormalArguments (BufferStringSet &) const
 
bool hasKey (const char *) const
 
bool getVal (const char *key, BufferString &, bool acceptnone=false, int valnr=1) const
 
template<class T >
bool getVal (const char *key, T &, bool acceptnone=false, int valnr=1) const
 
bool isPresent (const char *) const
 Is string present as an argument. More...
 
int nrArgs () const
 
bool isKey (int) const
 Does the arg start with - or –. More...
 
bool isKeyValue (int idx) const
 
const OD::StringgetArg (int idx) const
 
const OD::StringlastArg () const
 
const OD::StringgetExecutable () const
 
const OD::StringgetExecutableName () const
 

Static Public Member Functions

static void createKey (const char *key, BufferString &res)
 
static void addKey (const char *key, BufferString &cmd, const char *valstr=0)
 adds a space before but not after More...
 
static void addFilePath (const char *, BufferString &cmd)
 

Private Member Functions

int indexOf (const char *) const
 
void init (int, char **)
 
void init (const char *)
 

Private Attributes

BufferString progname_
 
BufferString executable_
 
BufferStringSet argv_
 
BufferStringSet keyswithvalue_
 
TypeSet< int > nrvalues_
 

Detailed Description

Parser that takes the argc and argv and makes them parsable. An argument starting with – is considered a key, as well as arguments starting with "-", if not imedialtely followed by a number. Hence -create is a key, -9 or -.3 are not.

"bin/my_prog --nriter 4 parfile1.par --fast parfile2.par" can be parsed as follows:

parser.setKeyHasValue("nriter"); //Makes it handle "4" as a value
const BufferString exec = parser.getExecutable(); //returns "bin/my_prog"
int nriter;
if ( !parser.getVal( "nriter", nriter ) )
return false;
const bool fast = parser.hasKey("fast");
BufferStringSet parfiles;
getNormalArguments( parfiles );
if ( parfiles.isEmpty() ) //Will have "parfile1.par" and "parfile2.par"
return false;

Constructor & Destructor Documentation

CommandLineParser::CommandLineParser ( const char *  )
CommandLineParser::CommandLineParser ( int  argc,
char **  argv 
)
CommandLineParser::CommandLineParser ( )

Actual command line is used, i.e. the one set by SetProgramArgs

Member Function Documentation

static void CommandLineParser::addFilePath ( const char *  ,
BufferString cmd 
)
static

adds "\ and \" to protect for spaces in FilePaths

static void CommandLineParser::addKey ( const char *  key,
BufferString cmd,
const char *  valstr = 0 
)
static

adds a space before but not after

static void CommandLineParser::createKey ( const char *  key,
BufferString res 
)
inlinestatic
const OD::String& CommandLineParser::getArg ( int  idx) const
inline
const OD::String& CommandLineParser::getExecutable ( ) const
const OD::String& CommandLineParser::getExecutableName ( ) const
void CommandLineParser::getNormalArguments ( BufferStringSet ) const

Gets all arguments that are not keys or key-values.

bool CommandLineParser::getVal ( const char *  key,
BufferString ,
bool  acceptnone = false,
int  valnr = 1 
) const
template<class T >
bool CommandLineParser::getVal ( const char *  key,
T &  val,
bool  acceptnone = false,
int  valnr = 1 
) const
inline

Will parse argument valnr following key. If acceptnone is true, it will only give error if key is found, but no value can be parsed.

bool CommandLineParser::hasKey ( const char *  ) const
int CommandLineParser::indexOf ( const char *  ) const
private
void CommandLineParser::init ( int  ,
char **   
)
private
void CommandLineParser::init ( const char *  )
private
bool CommandLineParser::isKey ( int  ) const

Does the arg start with - or –.

bool CommandLineParser::isKeyValue ( int  idx) const

True if not a key, and previous is a key that has been set using setKeyHasValue.

bool CommandLineParser::isPresent ( const char *  ) const

Is string present as an argument.

const OD::String& CommandLineParser::lastArg ( ) const
int CommandLineParser::nrArgs ( ) const
inline
Returns
the lump sum (keys, values, and everything else, but program name
void CommandLineParser::setKeyHasValue ( const char *  key,
int  nrvals = 1 
)

Tell the parser that the nrvals arguments after key are values. nrvals<1 denotes a variable number of values, running up to the next key. This function is only needed if you will use getNormalArguments.

Member Data Documentation

BufferStringSet CommandLineParser::argv_
private
BufferString CommandLineParser::executable_
private
BufferStringSet CommandLineParser::keyswithvalue_
private
TypeSet<int> CommandLineParser::nrvalues_
private
BufferString CommandLineParser::progname_
private

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019