ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GArgs.h
Revision: 16
Committed: Mon Jul 18 20:56:02 2011 UTC (13 years, 1 month ago) by gpertea
File size: 3993 byte(s)
Log Message:
sync with local source

Line File contents
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 #include <stdio.h>
18
19 struct GArgsDef {
20 const char* longopt;
21 char opt; //equivalent one-char option, if any
22 bool req_value; //true if the string that follows must be a value
23 int code; //an enum code to be associated with this option
24 };
25
26 class GArgs {
27 //structure for parsing arguments format definition
28 struct fmtdef {
29 char* longopt;
30 char opt; //equivalent one-char option, if any
31 bool req_value; //true if the string that follows must be a value
32 int code; //an enum code to be associated with this option
33 };
34 int fmtcount;
35 fmtdef* fmt; //this will store format definition after parsing it
36 struct argdata {
37 char* opt; // this is NULL for non-dashed arguments
38 // a single character for single dash style arguments
39 // a string for ARG=VALUE or --long_option style arguments
40 char* value; // is NULL for switches (dashed flags)
41 int fmti; //index in fmt table
42 //int code; // if GArgsDef[] constructor was used, for getOpt
43 };
44 int _argc;
45 char* const *_argv; //the original main() values
46 argdata* args; //arguments table after parsing it
47 int count; //total count of elements in 'args' array
48 int nonOptCount; //count of non-dashed, non= arguments
49 int nonOptPos; //current position for nonOpt arguments iterator
50 int optPos; //current position for options iterator
51 int errarg; //argv error position after parsing
52 bool err_valmissing; //if the error is strictly about missing value for errarg option
53 int parseArgs(bool nodigitopts=false);
54 //parsing helper functions
55 int validOpt(int c);
56 int validShortOpt(char o);
57 int validLongOpt(char* o, char* to);
58 public:
59
60 GArgs(int argc, char* const argv[], const char* format, bool nodigitopts=false);
61 /* format can be:
62 <string>{;|=} e.g. disable-test;PID=S= for --disable-test PID=50 (or --PID 50) S=3.5 etc.
63 <letter>[:] e.g. p:hT for -p testing (or -ptesting) -h -T
64 This means that the long options, if present, should be given at the beginning
65 of the format string, before the single-dash, single-char options
66 */
67 GArgs(int argc, char* const argv[], const GArgsDef fmtrecs[], bool nodigitopts=false);
68
69 ~GArgs();
70 int isError(); // returns the offending argv position or 0 if no error
71 int getCount() { return count; } //total number of arguments given
72 int getFmtCount() { return fmtcount; } //total number of option definitions
73 int getNonOptCount() { return nonOptCount; } //total number of non-option arguments
74 char* getOpt(const char* o); /* retrieve the value for option o
75 returns
76 NULL if option not given at all
77 !=NULL if boolean option was given
78 opt's value if value option was given
79 */
80 char* getOpt(const char o);
81 char* getOpt(int c); //retrieve value by enum code
82 char* getOptName(int c); //retrieve name of by enum code
83 int startOpt(); //init iteration through option arguments
84 // returns number of option args
85
86 char* nextOpt(); //get next option argument's string
87 int nextCode(); //get next option argument's code
88
89 int startNonOpt(void); //init iteration through non-option arguments
90 // returns the number of non-option arguments
91 void printError(FILE* fout, const char* usage=NULL,
92 bool exitProgram=false);
93 void printError(const char* usage=NULL, bool exitProgram=false);
94 void printCmdLine(FILE* fout);
95 char* nextNonOpt(); //get the next non-option argument
96 };
97
98 #endif