OpendTect 8.0
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
CommandLineParser Class Reference

#include <commandlineparser.h>

Public Member Functions

 CommandLineParser ()
 
 CommandLineParser (const char *)
 
 CommandLineParser (const CommandLineParser &)=delete
 
 CommandLineParser (int argc, char **argv)
 
virtual ~CommandLineParser ()
 
BufferString envVarBase () const
 
const OD::StringgetArg (int idx) const
 
const OD::StringgetExecutable () const
 
const OD::StringgetExecutableName () const
 
BufferString getFullSurveyPath (bool *iscursur=nullptr) 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
 
bool getVal (const char *key, FilePath &, 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 –.
 
bool isKeyValue (int idx) const
 
bool isPresent (const char *) const
 Is string present as an argument.
 
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
 
CommandLineParseroperator= (const CommandLineParser &)=delete
 
void setKeyHasValue (const char *key, int nrvals=1) const
 

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
 
static BufferString createKey (const char *key)
 
static void createKey (const char *key, BufferString &res)
 
static const char * sDataRootArg ()
 
static const char * sDefTransl ()
 
static const char * sNeedTempSurv ()
 
static const char * sSurveyArg ()
 

Private Member Functions

void ensureNrArgs (const char *, int) const
 
int indexOf (const char *, TypeSet< int > *idxs=nullptr) 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:24
bool isEmpty() const
Definition bufstringset.h:42
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size.
Definition bufstring.h:38
Definition commandlineparser.h:49
void getNormalArguments(BufferStringSet &) const
bool hasKey(const char *) const
const OD::String & getExecutable() const
bool getVal(const char *key, BufferString &, bool acceptnone=false, int valnr=1) const
void setKeyHasValue(const char *key, int nrvals=1) const

<>

Constructor & Destructor Documentation

◆ CommandLineParser() [1/4]

CommandLineParser::CommandLineParser ( const char * )

◆ CommandLineParser() [2/4]

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

◆ CommandLineParser() [3/4]

CommandLineParser::CommandLineParser ( )

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

◆ ~CommandLineParser()

virtual CommandLineParser::~CommandLineParser ( )
virtual

◆ CommandLineParser() [4/4]

CommandLineParser::CommandLineParser ( const CommandLineParser & )
delete

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 * iscursur = nullptr) const

◆ getNormalArguments()

void CommandLineParser::getNormalArguments ( BufferStringSet & ) const

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

◆ getVal() [1/6]

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

◆ getVal() [2/6]

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

◆ getVal() [3/6]

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

◆ getVal() [4/6]

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

◆ getVal() [5/6]

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() [6/6]

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()

int CommandLineParser::indexOf ( const char * ,
TypeSet< int > * idxs = nullptr ) 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

◆ operator=()

CommandLineParser & CommandLineParser::operator= ( const CommandLineParser & )
delete

◆ sDataRootArg()

static const char * CommandLineParser::sDataRootArg ( )
inlinestatic

◆ sDefTransl()

static const char * CommandLineParser::sDefTransl ( )
inlinestatic

◆ setKeyHasValue()

void CommandLineParser::setKeyHasValue ( const char * key,
int nrvals = 1 ) const

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.

◆ sNeedTempSurv()

static const char * CommandLineParser::sNeedTempSurv ( )
inlinestatic

◆ 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-2025