1 |
/* |
2 |
GArgs is a quick'n'dirty object oriented replacement for the standard |
3 |
getopts library call available on many unix platforms; |
4 |
it accepts the regular single dash style options |
5 |
-<letter>[ ][<value>] |
6 |
but also attr=value style options: |
7 |
<optname>=<value> |
8 |
*/ |
9 |
|
10 |
#ifndef G_ARGS_DEFINED |
11 |
#define G_ARGS_DEFINED |
12 |
|
13 |
#ifdef HAVE_CONFIG_H |
14 |
#include <config.h> |
15 |
#endif |
16 |
|
17 |
|
18 |
class GArgs { |
19 |
//structure for parsing arguments format definition |
20 |
struct fmtdef { |
21 |
int type; // 0=dashed switch, 1=dashed value, 2='=' value |
22 |
char* opt; //switch/opt char/string |
23 |
}; |
24 |
int fmtcount; |
25 |
fmtdef* fmt; //this will store format definition after parsing it |
26 |
|
27 |
struct argdata { |
28 |
char* opt; // this is NULL for non-dashed arguments |
29 |
//one character for dashed style arguments |
30 |
//one string for = style arguments |
31 |
char* value; // is NULL for switches (dashed flags) |
32 |
}; |
33 |
argdata* args; //arguments table after parsing it |
34 |
int count; //total count of elements in 'args' array |
35 |
int nonOptCount; //count of non-dashed, non= arguments |
36 |
int nonOptPos; //current position for nonOpt arguments iterator |
37 |
int optPos; //current position for options iterator |
38 |
int errarg; //argv error position after parsing |
39 |
static const char* is_opt; // = non NULL just for getOpt easy testing |
40 |
int validOpt(char o); //parsing helper function |
41 |
int validOpt(char* o); |
42 |
public: |
43 |
|
44 |
GArgs(int argc, char* const argv[], const char* format, bool nodigitopts=false); |
45 |
/* format is: |
46 |
<letter>[:] for e.g. p:hT <= -p testing -ptesting -h -T |
47 |
<string>= for e.g. PID=S= <= PID=50 S=3.5 |
48 |
This means that the = options, if present, must NEVER be given after |
49 |
dashed (non-value) switches directly |
50 |
*/ |
51 |
~GArgs(); |
52 |
int isError(); // returns the offending argv position or 0 if no error |
53 |
int getCount() { return count; } |
54 |
int getFmtCount() { return fmtcount; } |
55 |
int getNonOptCount() { return nonOptCount; } |
56 |
char* getOpt(const char* o); /* retrieve the value for option o |
57 |
returns |
58 |
NULL if option not given at all |
59 |
!=NULL if boolean option was given |
60 |
opt's value if value option was given |
61 |
*/ |
62 |
char* getOpt(const char o); |
63 |
int startOpt(); //init iteration through option arguments |
64 |
char* nextOpt(); //get next option argument |
65 |
int startNonOpt(); //init iteration through non-option arguments |
66 |
//returns the number of non-option arguments |
67 |
char* nextNonOpt(); //get the next non-option argument |
68 |
|
69 |
}; |
70 |
|
71 |
#endif |