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
/*
20110505 rsz Created.
*/
#include <stdio.h>
#include "ezOptionParser.hpp"

using namespace ez;

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

	opt.overview = "Full demo of all the features.";
	opt.syntax = "full [OPTIONS]";
	opt.example = "full -h\n\n";
	opt.footer = "full v0.1.4 Copyright (C) 2011 Remik Ziemlinski\nThis program is free and without warranty.\n";

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Print this usage message in one of three different layouts. The choices are:\n0 - aligned (default)\n1 - interleaved\n2 - staggered", // Help description.
		"-h",     // Flag token. 
		"-help", // Flag token.
		"--help", // Flag token.
		"--usage" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		0, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Do not print all input arguments for test. By default, all the inputs will be pretty printed to show which category they belong to (first, options, last) and what their values are. This shows that flag names can be arbitrary and don't need to begin with a single or double dash (-,--).", // Help description.
		"+d",     // Flag token. 
		"+dbg",  // Flag token.
		"+debug" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Test integer input.", // Help description.
		"-n",     // Flag token. 
		"-int", 	// Flag token.
		"--int", 	// Flag token.
		"--integer" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Test long input.", // Help description.
		"-l",     // Flag token. 
		"-lng", 	// Flag token.
		"-long", 	// Flag token.
		"--long" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Test unsigned long input.", // Help description.
		"-u",     // Flag token. 
		"-ulong", // Flag token.
		"--ulong" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Test float input.", // Help description.
		"-f",     // Flag token. 
		"-flt", 	// Flag token.
		"-float", // Flag token.
		"--float" // Flag token.
	);

	opt.add(
		"0", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Test double input.", // Help description.
		"-d",     // Flag token. 
		"-dbl", 	// Flag token.
		"-double", // Flag token.
		"--double" // Flag token.
	);

	opt.add(
		"A default string.", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Test string input.", // Help description.
		"-s",     // Flag token. 
		"-str", 	// Flag token.
		"-string", 	// Flag token.
		"--string" // Flag token.
	);

	opt.add(
		"0,1,2,3,4,5,6,7,8,9,10", // Default.
		0, // Required?
		1, // Number of args expected.
		',', // Delimiter if expecting multiple args.
		"Test integer list input delimited with comma.", // Help description.
		"-nl",     // Flag token. 
		"-nlist", 	// Flag token.
		"-intlist", 	// Flag token.
		"--intlist" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		':', // Delimiter if expecting multiple args.
		"Test long list input delimited by colon.", // Help description.
		"-ll",     // Flag token. 
		"-llist", 	// Flag token.
		"-longlist", 	// Flag token.
		"--longlist" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		',', // Delimiter if expecting multiple args.
		"Test unsigned long list input.", // Help description.
		"-ul",     // Flag token. 
		"-ulist", 	// Flag token.
		"-ulonglist", 	// Flag token.
		"--ulonglist" // Flag token.
	);

	opt.add(
		"-2.1e-20,-.2,-.001,0,1,1e10", // Default.
		0, // Required?
		1, // Number of args expected.
		',', // Delimiter if expecting multiple args.
		"Test float list input.", // Help description.
		"-fl",     // Flag token. 
		"-flist", 	// Flag token.
		"-floatlist", 	// Flag token.
		"--floatlist" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		',', // Delimiter if expecting multiple args.
		"Test double list input.", // Help description.
		"-dl",     // Flag token. 
		"-dlist", 	// Flag token.
		"-dbllist", 	// Flag token.
		"--doublelist" // Flag token.
	);

	opt.add(
		"\"string list item1\",item2,\"list # item3\",2000/1/1", // Default.
		0, // Required?
		1, // Number of args expected.
		',', // Delimiter if expecting multiple args.
		"Test string list input.", // Help description.
		"-sl",     // Flag token. 
		"-slist", 	// Flag token.
		"-strlist", 	// Flag token.
		"--strlist" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Import additional arguments from file. Multiple files can be imported by using multiple instances of this option. The file options will add to those set on the command line. If you want them to overwrite the command line options, then use +import.", // Help description.
		"-i",     // Flag token. 
		"-import" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Import additional arguments from file. Multiple files can be imported by using multiple instances of this option. The last file will overwrite all options set previously by either command line or another file. If you want them to add to previously set options, then use -import.", // Help description.
		"+i",     // Flag token. 
		"+import" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Export only set arguments to file.", // Help description.
		"-e",     // Flag token. 
		"-export" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		1, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Export all arguments, including defaults, to file.", // Help description.
		"+e",     // Flag token. 
		"+export" // Flag token.
	);

	opt.parse(argc, argv);

	if (opt.isSet("-h")) {
		std::string usage;
		int layout;
		opt.get("-h")->getInt(layout);
		switch(layout) {
		case 0:
			opt.getUsage(usage,80,ezOptionParser::ALIGN);
			break;
		case 1:
			opt.getUsage(usage,80,ezOptionParser::INTERLEAVE);
			break;
		case 2:
			opt.getUsage(usage,80,ezOptionParser::STAGGER);
			break;
		}
		std::cout << usage;
		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;
	}

	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);

		if(!files.empty()) {
			std::string file = files[files.size()-1][0];
			opt.resetArgs();
			if (! opt.importFile(file.c_str(), '#'))
				std::cerr << "ERROR: Failed to open file " << file << std::endl;
		}
	}

	if (opt.isSet("-e")) {
		std::string outfile;
		opt.get("-e")->getString(outfile);
		opt.exportFile(outfile.c_str(), false);
	}

	if (opt.isSet("+e")) {
		std::string outfile;
		opt.get("+e")->getString(outfile);
		opt.exportFile(outfile.c_str(), true);
	}

	if (!opt.isSet("+d")) {
		std::string pretty;
		opt.prettyPrint(pretty);
		std::cout << pretty;
	}

	return 0;
}