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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
20111007 rsz Created.
*/
#include <stdio.h>
#include "ezOptionParser.hpp"

using namespace ez;

void Usage(ezOptionParser& opt) {
  std::string usage;
  opt.getUsage(usage);
  std::cout << usage;
};

int main(int argc, const char * argv[]) {
  ezOptionParser opt;

  opt.overview = "Demo of validation options using native datatype parameters.";
  opt.syntax = "validfast [OPTIONS]";
  opt.example = "validfast -i validin1.txt\n\n";

  opt.add(
    "", // Default.
    0, // Required?
    0, // Number of args expected.
    0, // Delimiter if expecting multiple args.
    "Display usage instructions.", // Help description.
    "-h",     // Flag token. 
    "--help", // Flag token.
    "--usage" // Flag token.
  );

  ezOptionValidator* vS1 = new ezOptionValidator(ezOptionValidator::S1);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Signed byte (aka char, 1 byte) between -128 and 127.", // Help description.
    "-s1",     // Flag token.
    vS1
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of signed byte (aka char, 1 byte) between -128 and 127.", // Help description.
    "-s1list",     // Flag token.
    vS1
  );

  ezOptionValidator* vU1 = new ezOptionValidator(ezOptionValidator::U1);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Unsigned byte (aka unsigned char, 1 byte) between 0 and 255.", // Help description.
    "-u1",     // Flag token.
    vU1
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of unsigned byte (aka unsigned char, 1 byte) between 0 and 255.", // Help description.
    "-u1list",     // Flag token.
    vU1
  );

  ezOptionValidator* vS2 = new ezOptionValidator(ezOptionValidator::S2);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Short (2 bytes) between -32768 and 32767.", // Help description.
    "-s2",     // Flag token.
    vS2
  );
  
  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of short (2 bytes) between -32768 and 32767.", // Help description.
    "-s2list",     // Flag token.
    vS2
  );

  ezOptionValidator* vU2 = new ezOptionValidator(ezOptionValidator::U2);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Unsigned short (2 bytes) between 0 and 65535.", // Help description.
    "-u2",     // Flag token.
    vU2
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of unsigned short (2 bytes) between 0 and 65535.", // Help description.
    "-u2list",     // Flag token.
    vU2
  );

  ezOptionValidator* vS4 = new ezOptionValidator(ezOptionValidator::S4);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Integer (4 bytes) between -2147483648 and 2147483647.", // Help description.
    "-s4",     // Flag token.
    vS4
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of integer (4 bytes) between -2147483648 and 2147483647.", // Help description.
    "-s4list",     // Flag token.
    vS4
  );

  ezOptionValidator* vU4 = new ezOptionValidator(ezOptionValidator::U4);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Unsigned int (4 bytes) between 0 and 4294967295.", // Help description.
    "-u4",     // Flag token.
    vU4
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of unsigned int (4 bytes) between 0 and 4294967295.", // Help description.
    "-u4list",     // Flag token.
    vU4
  );
  
  ezOptionValidator* vU8 = new ezOptionValidator(ezOptionValidator::U8);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Unsigned long long (8 bytes) between 0 and 18446744073709551615.", // Help description.
    "-u8",     // Flag token.
    vU8
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of unsigned long long (8 bytes) between 0 and 18446744073709551615.", // Help description.
    "-u8list",     // Flag token.
    vU8
  );

  ezOptionValidator* vS8 = new ezOptionValidator(ezOptionValidator::S8);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Long long (68 bytes) between -9223372036854775808 and 9223372036854775807.", // Help description.
    "-s8",     // Flag token.
    vS8
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of long long (68 bytes) between -9223372036854775808 and 9223372036854775807.", // Help description.
    "-s8list",     // Flag token.
    vS8
  );

  ezOptionValidator* vF = new ezOptionValidator(ezOptionValidator::F);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Float (4 bytes) between -3.40282e+038 and 3.40282e+038.", // Help description.
    "-f",     // Flag token.
    vF
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of float (4 bytes) between -3.40282e+038 and 3.40282e+038.", // Help description.
    "-flist",     // Flag token.
    vF
  );

  ezOptionValidator* vD = new ezOptionValidator(ezOptionValidator::D);
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Double (8 bytes) between -1.79769e+308 and 1.79769e+308.", // Help description.
    "-d",     // Flag token.
    vD
  );

  opt.add(
    "0", // Default.
    0, // Required?
    -1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "List of double (8 bytes) between -1.79769e+308 and 1.79769e+308.", // Help description.
    "-dlist",     // Flag token.
    vD
  );
  
  const char* boolStrings[] = {"0","1","n","y","f","t","no","yes","false","true"};
  ezOptionValidator* vBool = new ezOptionValidator(ezOptionValidator::T, ezOptionValidator::IN, boolStrings, 10, true);
  
  opt.add(
    "0", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "Boolean string. Either one of these (case insensitive):\n0,1,n,y,f,t,no,yes,false,true", // Help description.
    "-b",     // Flag token.
    vBool
  );
  
  opt.add(
    "", // Default.
    0, // Required?
    1, // Number of args expected.
    ',', // Delimiter if expecting multiple args.
    "File with input options to aid stress testing.", // Help description.
    "-i" // Flag token.
  );

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"File to export options.", // Help description.
		"-o"     // Flag token. 
	);

  opt.parse(argc, argv);

  if (opt.isSet("-h")) {
    Usage(opt);
    return 1;
  }

	if (opt.isSet("-i")) {
		// Import one or more files that use # as comment char.
		std::vector< std::vector<std::string> > files;
		opt.get("-i")->getMultiStrings(files);

		for(int j=0; j < files.size(); ++j)
			if (! opt.importFile(files[j][0].c_str(), '#'))
				std::cerr << "ERROR: Failed to open file " << files[j][0] << std::endl;
	}

  std::vector<std::string> badOptions;
  int i;
  if(!opt.gotRequired(badOptions)) {
    for(i=0; i < badOptions.size(); ++i)
      std::cerr << "ERROR: Missing required option " << badOptions[i] << ".\n\n";
    Usage(opt);
    return 1;
  }

  if(!opt.gotExpected(badOptions)) {
    for(i=0; i < badOptions.size(); ++i)
      std::cerr << "ERROR: Got unexpected number of arguments for option " << badOptions[i] << ".\n\n";
      
    Usage(opt);
    return 1;
  }

  std::vector<std::string> badArgs;
  if(!opt.gotValid(badOptions, badArgs)) {
    for(i=0; i < badOptions.size(); ++i)
      std::cerr << "ERROR: Got invalid argument \"" << badArgs[i] << "\" for option " << badOptions[i] << ".\n\n";
      
    //Usage(opt);
    return 1;
  }
  
	if (opt.isSet("-o")) {
		std::string file;
		opt.get("-o")->getString(file);
		// Exports all options if second param is true; unset options will just use their default values.
		opt.exportFile(file.c_str(), false);
	}

  return 0;
}