OpendTect-6_4  6.4
commandlineparser.h
Go to the documentation of this file.
1 #ifndef commandlineparser_h
2 #define commandlineparser_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: K. Tingdahl
9  Date: March 2013
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 
14 
15 -*/
16 
17 #include "genc.h"
18 #include "bufstringset.h"
19 #include "typeset.h"
20 #include "debug.h" // easier for test programs, declares od_init_test_program
21 
52 {
53 public:
54  CommandLineParser(const char*);
55  CommandLineParser(int argc,char** argv);
56  CommandLineParser();
60  void setKeyHasValue(const char* key,int nrvals=1);
66  void getNormalArguments(BufferStringSet&) const;
70  bool hasKey(const char*) const;
71  bool getVal(const char* key,BufferString&,
72  bool acceptnone=false,int valnr=1) const;
73  template <class T> bool getVal(const char* key,T&,
74  bool acceptnone=false,int valnr=1) const;
80  bool isPresent(const char*) const;
82 
83  int nrArgs() const { return argv_.size(); }
87  bool isKey(int) const;
89  bool isKeyValue(int idx) const;
93  const OD::String& getArg(int idx) const { return *argv_[idx]; }
94  const OD::String& lastArg() const;
95 
96  const OD::String& getExecutable() const;
97  const OD::String& getExecutableName() const;
98 
99  static void createKey(const char* key,BufferString& res)
100  { res = "--"; res += key; }
101  static void addKey(const char* key,BufferString& cmd,
102  const char* valstr=0);
104  static void addFilePath(const char*,BufferString& cmd);
109 private:
110 
111  int indexOf(const char*) const;
112  void init(int,char**);
113  void init(const char*);
114 
118 
121 };
122 
123 //Implementation
124 
125 template <class T> inline
126 bool CommandLineParser::getVal( const char* key, T& val,
127  bool acceptnone, int valnr ) const
128 {
129  const int keyidx = indexOf( key );
130  if ( keyidx<0 )
131  return acceptnone;
132 
133  const int validx = keyidx + mMAX(valnr,1);
134  if ( !argv_.validIdx( validx ) || isKey(validx) )
135  return false;
136 
137  return getFromString( val, argv_[validx]->buf(), mUdf(T) );
138 }
139 
140 
141 #endif
#define mExpClass(module)
Definition: commondefs.h:160
bool getFromString(int &, const char *, int udfval)
static void createKey(const char *key, BufferString &res)
Definition: commandlineparser.h:99
bool init()
BufferStringSet keyswithvalue_
Definition: commandlineparser.h:119
BufferString executable_
Definition: commandlineparser.h:116
Set of BufferString objects.
Definition: bufstringset.h:28
ObjectSet< T >::size_type indexOf(const ObjectSet< T > &os, const S &val)
Locate object in set.
Definition: objectset.h:169
int indexOf(const char *) const
Definition: commandlineparser.h:51
TypeSet< int > nrvalues_
Definition: commandlineparser.h:120
bool isKey(int) const
Does the arg start with - or –.
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
encapsulates the read-access-only part of strings in OD.
Definition: odstring.h:31
const OD::String & getArg(int idx) const
Definition: commandlineparser.h:93
BufferStringSet argv_
Definition: commandlineparser.h:117
bool getVal(const char *key, BufferString &, bool acceptnone=false, int valnr=1) const
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:40
int nrArgs() const
Definition: commandlineparser.h:83
virtual bool validIdx(int64_t) const
Definition: objectset.h:302
BufferString progname_
Definition: commandlineparser.h:115
#define mMAX(x, y)
Definition: commondefs.h:48

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