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
/*
20110505 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 parser's features.";
	opt.syntax = "complete first second [OPTIONS] in1 [... inN] out";
	opt.example = "complete a b -f --list 1,2,3 --list 4,5,6,7,8 -s string -int -2147483648,2147483647 -ulong 9223372036854775807 -float 3.40282e+038 -double 1.79769e+308 f1 f2 f3 f4 f5 f6 fout\n\n";
	opt.footer = "ezOptionParser 0.1.4  Copyright (C) 2011 Remik Ziemlinski\nThis program is free and without warranty.\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.
		"--help", // Flag token.
		"--usage" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		0, // Number of args expected.
		0, // Delimiter if expecting multiple args.
		"Simple flag with a very long help description that will be split automatically into a two column format when usage is printed for this program. Newlines will also help with justification.\nFor example:\n0 - an item\n1 - another item\n2 - and another item", // Help description.
		"-f",     // Flag token. 
		"-flg",   // Flag token.
		"--flag" // Flag token.
	);

	opt.add(
		"", // Default.
		0, // Required?
		-1, // Number of args expected.
		',', // Delimiter if expecting multiple args.
		"Lists of arbitrary lengths.", // Help description.
		"-l",    // Flag token. 
		"-lst",  // Flag token.
		"-list",  // Flag token.
		"--list" // Flag token.
	);

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

	opt.add(
		"0,1", // Default.
		0, // Required?
		2, // Number of args expected.
		',', // Delimiter if expecting multiple args.
		"Integer placeholder.", // Help description.
		"-i", // Flag token.
		"-int", // Flag token.
		"-integer", // Flag token.
		"--integer" // Flag token.
	);

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

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

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

	opt.parse(argc, argv);

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

	if (opt.lastArgs.size() < 2) {
		std::cerr << "ERROR: Expected at least 2 arguments.\n\n";
		Usage(opt);
		return 1;
	} 

	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::string firstArg;
	if (opt.firstArgs.size() > 0)
		firstArg = *opt.firstArgs[0];
		
	bool flag = false;
	if (opt.isSet("-f")) {
		flag = true;
	}

	std::vector<int> list;
	opt.get("-lst")->getInts(list);
	std::cout << "\nList:";
	for(int j=0; j < list.size(); ++j)
		std::cout << " " << list[j];
		
	std::cout << std::endl;

	return 0;
}