-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathanyoption.h
271 lines (227 loc) · 7.9 KB
/
anyoption.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#ifndef _ANYOPTION_H
#define _ANYOPTION_H
#define _CRT_SECURE_NO_WARNINGS /* Microsoft C/C++ Compiler: Disable C4996 \
warnings for security-enhanced CRT \
functions */
#include <cstring>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <string>
enum OptionType {
INVALID_OPT = 0,
COMMON_OPT = 1,
COMMAND_OPT = 2,
FILE_OPT = 3,
COMMON_FLAG = 4,
COMMAND_FLAG = 5,
FILE_FLAG = 6,
};
enum {
DEFAULT_MAXOPTS=10,
MAX_LONG_PREFIX_LENGTH=2,
DEFAULT_MAXUSAGE=3,
DEFAULT_MAXHELP=10,
};
#define TRUE_FLAG "true"
using namespace std;
class AnyOption {
public: /* the public interface */
AnyOption();
explicit AnyOption(unsigned int maxoptions);
explicit AnyOption(unsigned int maxoptions, unsigned int maxcharoptions);
~AnyOption();
/*
* following set methods specifies the
* special characters and delimiters
* if not set traditional defaults will be used
*/
void setCommandPrefixChar(char _prefix); /* '-' in "-w" */
void setCommandLongPrefix(const char *_prefix); /* '--' in "--width" */
void setFileCommentChar(char _comment); /* '#' in shell scripts */
void setFileDelimiterChar(char _delimiter); /* ':' in "width : 100" */
/*
* provide the input for the options
* like argv[] for commnd line and the
* option file name to use;
*/
void useCommandArgs(int _argc, char **_argv);
void useFiileName(const char *_filename);
/*
* turn off the POSIX style options
* this means anything starting with a '-' or "--"
* will be considered a valid option
* which also means you cannot add a bunch of
* POIX options chars together like "-lr" for "-l -r"
*
*/
void noPOSIX();
/*
* prints warning verbose if you set anything wrong
*/
void setVerbose();
/*
* there are two types of options
*
* Option - has an associated value ( -w 100 )
* Flag - no value, just a boolean flag ( -nogui )
*
* the options can be either a string ( GNU style )
* or a character ( traditional POSIX style )
* or both ( --width, -w )
*
* the options can be common to the command line and
* the option file, or can belong only to either of
* command line and option file
*
* following set methods, handle all the above
* cases of options.
*/
/* options command to command line and option file */
void setOption(const char *opt_string);
void setOption(char opt_char);
void setOption(const char *opt_string, char opt_char);
void setFlag(const char *opt_string);
void setFlag(char opt_char);
void setFlag(const char *opt_string, char opt_char);
/* options read from command line only */
void setCommandOption(const char *opt_string);
void setCommandOption(char opt_char);
void setCommandOption(const char *opt_string, char opt_char);
void setCommandFlag(const char *opt_string);
void setCommandFlag(char opt_char);
void setCommandFlag(const char *opt_string, char opt_char);
/* options read from an option file only */
void setFileOption(const char *opt_string);
void setFileOption(char opt_char);
void setFileOption(const char *opt_string, char opt_char);
void setFileFlag(const char *opt_string);
void setFileFlag(char opt_char);
void setFileFlag(const char *opt_string, char opt_char);
/*
* process the options, registered using
* useCommandArgs() and useFileName();
*/
void processOptions();
void processCommandArgs();
void processCommandArgs(unsigned int max_args);
bool processFile();
/*
* process the specified options
*/
void processCommandArgs(int _argc, char **_argv);
void processCommandArgs(int _argc, char **_argv, int max_args);
bool processFile(const char *_filename);
/*
* get the value of the options
* will return NULL if no value is set
*/
char *getValue(const char *_option);
bool getFlag(const char *_option);
char *getValue(char _optchar);
bool getFlag(char _optchar);
/*
* Print Usage
*/
void printUsage();
void printAutoUsage();
void addUsage(const char *line);
void printHelp();
/* print auto usage printing for unknown options or flag */
void autoUsagePrint(bool flag);
/*
* get the argument count and arguments sans the options
*/
int getArgc() const;
char *getArgv(int index) const;
bool hasOptions() const;
private: /* the hidden data structure */
int argc; /* command line arg count */
char **argv; /* commnd line args */
const char *filename; /* the option file */
char *appname; /* the application name from argv[0] */
int *new_argv; /* arguments sans options (index to argv) */
int new_argc; /* argument count sans the options */
unsigned int max_legal_args; /* ignore extra arguments */
/* option strings storage + indexing */
unsigned int max_options; /* maximum number of options */
const char **options; /* storage */
OptionType *optiontype; /* type - common, command, file */
int *optionindex; /* index into value storage */
unsigned int option_counter; /* counter for added options */
/* option chars storage + indexing */
unsigned int max_char_options; /* maximum number options */
char *optionchars; /* storage */
OptionType *optchartype; /* type - common, command, file */
int *optcharindex; /* index into value storage */
int optchar_counter; /* counter for added options */
/* values */
char **values; /* common value storage */
int g_value_counter; /* globally updated value index LAME! */
/* help and usage */
const char **usage; /* usage */
unsigned int max_usage_lines; /* max usage lines reserved */
unsigned int usage_lines; /* number of usage lines */
bool command_set; /* if argc/argv were provided */
bool file_set; /* if a filename was provided */
bool mem_allocated; /* if memory allocated in init() */
bool posix_style; /* enables to turn off POSIX style options */
bool verbose; /* silent|verbose */
bool print_usage; /* usage verbose */
bool print_help; /* help verbose */
char opt_prefix_char; /* '-' in "-w" */
char long_opt_prefix[MAX_LONG_PREFIX_LENGTH + 1]; /* '--' in "--width" */
char file_delimiter_char; /* ':' in width : 100 */
char file_comment_char; /* '#' in "#this is a comment" */
char equalsign;
char comment;
char delimiter;
char endofline;
char whitespace;
char nullterminate;
bool set; // was static member
bool once; // was static member
bool hasoptions;
bool autousage;
private: /* the hidden utils */
void init();
void init(unsigned int maxopt, unsigned int maxcharopt);
bool alloc();
void allocValues(int index, size_t length);
void cleanup();
bool valueStoreOK();
/* grow storage arrays as required */
bool doubleOptStorage();
bool doubleCharStorage();
bool doubleUsageStorage();
bool setValue(const char *option, char *value);
bool setFlagOn(const char *option);
bool setValue(char optchar, char *value);
bool setFlagOn(char optchar);
void addOption(const char *option, OptionType type);
void addOption(char optchar, OptionType type);
void addOptionError(const char *opt) const;
void addOptionError(char opt) const;
bool findFlag(char *value);
void addUsageError(const char *line);
bool CommandSet() const;
bool FileSet() const;
bool POSIX() const;
char parsePOSIX(char *arg);
int parseGNU(char *arg);
bool matchChar(char c);
int matchOpt(char *opt);
/* dot file methods */
char *readFile();
char *readFile(const char *fname);
bool consumeFile(char *buffer);
void processLine(char *theline, int length);
char *chomp(char *str);
void valuePairs(char *type, char *value);
void justValue(char *value);
void printVerbose(const char *msg) const;
void printVerbose(char *msg) const;
void printVerbose(char ch) const;
void printVerbose() const;
};
#endif /* ! _ANYOPTION_H */