OpendTect  6.6
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
CommandLineParser Class Reference

Public Member Functions

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

Static Public Member Functions

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

Private Member Functions

void ensureNrArgs (const char *, int) const
 
int indexOf (const char *) const
 
int indexOf (const char *, TypeSet< int > *idxs) const
 
void init (const char *)
 
void init (int, char **)
 

Private Attributes

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

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;
Set of BufferString objects.
Definition: bufstringset.h:26
bool isEmpty() const
Definition: bufstringset.h:44
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size.
Definition: bufstring.h:40
Definition: commandlineparser.h:53
void getNormalArguments(BufferStringSet &) const
bool hasKey(const char *) const
const OD::String & getExecutable() const
void setKeyHasValue(const char *key, int nrvals=1)
bool getVal(const char *key, BufferString &, bool acceptnone=false, int valnr=1) const

<>

Constructor & Destructor Documentation

◆ CommandLineParser() [1/3]

CommandLineParser::CommandLineParser ( const char *  )

◆ CommandLineParser() [2/3]

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

◆ CommandLineParser() [3/3]

CommandLineParser::CommandLineParser ( )

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

Member Function Documentation

◆ addFilePath()

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

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

◆ addKey()

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

adds a space before but not after

◆ createKey() [1/2]

static BufferString CommandLineParser::createKey ( const char *  key)
inlinestatic

◆ createKey() [2/2]

static void CommandLineParser::createKey ( const char *  key,
BufferString res 
)
inlinestatic

◆ ensureNrArgs()

void CommandLineParser::ensureNrArgs ( const char *  ,
int   
) const
private

◆ envVarBase()

BufferString CommandLineParser::envVarBase ( ) const

◆ getArg()

const OD::String& CommandLineParser::getArg ( int  idx) const
inline

◆ getExecutable()

const OD::String& CommandLineParser::getExecutable ( ) const

◆ getExecutableName()

const OD::String& CommandLineParser::getExecutableName ( ) const

◆ getFullSurveyPath()

BufferString CommandLineParser::getFullSurveyPath ( bool *  iscursurv = 0) const

◆ getNormalArguments()

void CommandLineParser::getNormalArguments ( BufferStringSet ) const

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

◆ getVal() [1/5]

bool CommandLineParser::getVal ( const char *  key,
BufferString ,
bool  acceptnone = false,
int  valnr = 1 
) const

◆ getVal() [2/5]

bool CommandLineParser::getVal ( const char *  key,
BufferStringSet ,
bool  acceptnone = false 
) const

◆ getVal() [3/5]

bool CommandLineParser::getVal ( const char *  key,
DBKey ,
bool  acceptnone = false,
int  valnr = 1 
) const

◆ getVal() [4/5]

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.

◆ getVal() [5/5]

template<class T >
bool CommandLineParser::getVal ( const char *  key,
TypeSet< T > &  vals,
bool  acceptnone = false 
) const
inline

◆ hasKey()

bool CommandLineParser::hasKey ( const char *  ) const

◆ indexOf() [1/2]

int CommandLineParser::indexOf ( const char *  ) const
private

◆ indexOf() [2/2]

int CommandLineParser::indexOf ( const char *  ,
TypeSet< int > *  idxs 
) const
private

◆ init() [1/2]

void CommandLineParser::init ( const char *  )
private

◆ init() [2/2]

void CommandLineParser::init ( int  ,
char **   
)
private

◆ isKey()

bool CommandLineParser::isKey ( int  ) const

Does the arg start with - or –.

◆ isKeyValue()

bool CommandLineParser::isKeyValue ( int  idx) const

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

◆ isPresent()

bool CommandLineParser::isPresent ( const char *  ) const

Is string present as an argument.

◆ keyedString()

BufferString CommandLineParser::keyedString ( const char *  ky,
int  argnr = 0 
) const
inline

◆ keyedValue()

template<class T >
T CommandLineParser::keyedValue ( const char *  ky,
int  argnr = 0 
) const
inline

◆ lastArg()

const OD::String& CommandLineParser::lastArg ( ) const

◆ nrArgs()

int CommandLineParser::nrArgs ( ) const
inline
Returns
the lump sum (keys, values, and everything else, but program name

◆ sDataRootArg()

static const char* CommandLineParser::sDataRootArg ( )
inlinestatic

◆ setKeyHasValue()

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.

◆ sSurveyArg()

static const char* CommandLineParser::sSurveyArg ( )
inlinestatic

Member Data Documentation

◆ argv_

BufferStringSet CommandLineParser::argv_
private

◆ executable_

BufferString CommandLineParser::executable_
private

◆ keyswithvalue_

BufferStringSet CommandLineParser::keyswithvalue_
private

◆ nrvalues_

TypeSet<int> CommandLineParser::nrvalues_
private

◆ progname_

BufferString CommandLineParser::progname_
private

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