-
Notifications
You must be signed in to change notification settings - Fork 18
/
fullSample.cpp
266 lines (247 loc) · 10.2 KB
/
fullSample.cpp
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
// File: fullSample.cpp
// Library: SimpleOpt
// Author: Brodie Thiesfield <[email protected]>
// Source: http://code.jellycan.com/simpleopt/
//
// MIT LICENCE
// ===========
// The licence text below is the boilerplate "MIT Licence" used from:
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) 2006-2007, Brodie Thiesfield
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#if defined(_MSC_VER)
# include <windows.h>
# include <tchar.h>
#else
# define TCHAR char
# define _T(x) x
# define _tprintf printf
# define _tmain main
# define _ttoi atoi
#endif
#include <stdio.h>
//#define SO_MAX_ARGS 54 // fixed size, no C lib use
#include "SimpleOpt.h"
#include "SimpleGlob.h"
static void ShowUsage()
{
_tprintf(
_T("Usage: fullSample [OPTIONS] [FILES]\n")
_T("\n")
_T("--exact Disallow partial matching of option names\n")
_T("--noslash Disallow use of slash as an option marker on Windows\n")
_T("--shortarg Permit arguments on single letter options with no equals sign\n")
_T("--clump Permit single char options to be clumped as long string\n")
_T("--noerr Do not generate any errors for invalid options\n")
_T("--pedantic Generate an error for petty things\n")
_T("--icase Case-insensitive for all types\n")
_T("--icase-short Case-insensitive for short args\n")
_T("--icase-long Case-insensitive for long argsn")
_T("--icase-word Case-insensitive for word args\n")
_T("\n")
_T("-d -e -E -f -F -g -flag --flag Flag (no arg)\n")
_T("-s ARG -sep ARG --sep ARG Separate required arg\n")
_T("-S ARG -SEP ARG --SEP ARG Separate required arg (uppercase)\n")
_T("-cARG -c=ARG -com=ARG --com=ARG Combined required arg\n")
_T("-o[ARG] -o[=ARG] -opt[=ARG] --opt[=ARG] Combined optional arg\n")
_T("-man -mandy -mandate Shortcut matching tests\n")
_T("--man --mandy --mandate Shortcut matching tests\n")
_T("--multi0 --multi1 ARG --multi2 ARG1 ARG2 Multiple argument tests\n")
_T("--multi N ARG-1 ARG-2 ... ARG-N Multiple argument tests\n")
_T("-- Stop argument processing\n")
_T("open read write close zip unzip UPCASE Special words\n")
_T("\n")
_T("-? -h -help --help Output this help.\n")
_T("\n")
_T("If a FILE is `-', read standard input.\n")
);
}
CSimpleOpt::SOption g_rgFlags[] =
{
{ SO_O_EXACT, _T("--exact"), SO_NONE },
{ SO_O_NOSLASH, _T("--noslash"), SO_NONE },
{ SO_O_SHORTARG, _T("--shortarg"), SO_NONE },
{ SO_O_CLUMP, _T("--clump"), SO_NONE },
{ SO_O_NOERR, _T("--noerr"), SO_NONE },
{ SO_O_PEDANTIC, _T("--pedantic"), SO_NONE },
{ SO_O_ICASE, _T("--icase"), SO_NONE },
{ SO_O_ICASE_SHORT, _T("--icase-short"), SO_NONE },
{ SO_O_ICASE_LONG, _T("--icase-long"), SO_NONE },
{ SO_O_ICASE_WORD, _T("--icase-word"), SO_NONE },
SO_END_OF_OPTIONS
};
enum { OPT_HELP = 0, OPT_MULTI = 100, OPT_MULTI0, OPT_MULTI1, OPT_MULTI2, OPT_STOP };
CSimpleOpt::SOption g_rgOptions[] =
{
{ OPT_HELP, _T("-?"), SO_NONE },
{ OPT_HELP, _T("-h"), SO_NONE },
{ OPT_HELP, _T("-help"), SO_NONE },
{ OPT_HELP, _T("--help"), SO_NONE },
{ 1, _T("-"), SO_NONE },
{ 2, _T("-d"), SO_NONE },
{ 3, _T("-e"), SO_NONE },
{ 4, _T("-f"), SO_NONE },
{ 5, _T("-g"), SO_NONE },
{ 6, _T("-flag"), SO_NONE },
{ 7, _T("--flag"), SO_NONE },
{ 8, _T("-s"), SO_REQ_SEP },
{ 9, _T("-sep"), SO_REQ_SEP },
{ 10, _T("--sep"), SO_REQ_SEP },
{ 11, _T("-c"), SO_REQ_CMB },
{ 12, _T("-com"), SO_REQ_CMB },
{ 13, _T("--com"), SO_REQ_CMB },
{ 14, _T("-o"), SO_OPT },
{ 15, _T("-opt"), SO_OPT },
{ 16, _T("--opt"), SO_OPT },
{ 17, _T("-man"), SO_NONE },
{ 18, _T("-mandy"), SO_NONE },
{ 19, _T("-mandate"), SO_NONE },
{ 20, _T("--man"), SO_NONE },
{ 21, _T("--mandy"), SO_NONE },
{ 22, _T("--mandate"), SO_NONE },
{ 23, _T("open"), SO_NONE },
{ 24, _T("read"), SO_NONE },
{ 25, _T("write"), SO_NONE },
{ 26, _T("close"), SO_NONE },
{ 27, _T("zip"), SO_NONE },
{ 28, _T("unzip"), SO_NONE },
{ 29, _T("-E"), SO_NONE },
{ 30, _T("-F"), SO_NONE },
{ 31, _T("-S"), SO_REQ_SEP },
{ 32, _T("-SEP"), SO_REQ_SEP },
{ 33, _T("--SEP"), SO_REQ_SEP },
{ 34, _T("UPCASE"), SO_NONE },
{ OPT_MULTI, _T("--multi"), SO_MULTI },
{ OPT_MULTI0, _T("--multi0"), SO_MULTI },
{ OPT_MULTI1, _T("--multi1"), SO_MULTI },
{ OPT_MULTI2, _T("--multi2"), SO_MULTI },
{ OPT_STOP, _T("--"), SO_NONE },
SO_END_OF_OPTIONS
};
void ShowFiles(int argc, TCHAR ** argv) {
// glob files to catch expand wildcards
CSimpleGlob glob(SG_GLOB_NODOT|SG_GLOB_NOCHECK);
if (SG_SUCCESS != glob.Add(argc, argv)) {
_tprintf(_T("Error while globbing files\n"));
return;
}
for (int n = 0; n < glob.FileCount(); ++n) {
_tprintf(_T("file %2d: '%s'\n"), n, glob.File(n));
}
}
static const TCHAR *
GetLastErrorText(
int a_nError
)
{
switch (a_nError) {
case SO_SUCCESS: return _T("Success");
case SO_OPT_INVALID: return _T("Unrecognized option");
case SO_OPT_MULTIPLE: return _T("Option matched multiple strings");
case SO_ARG_INVALID: return _T("Option does not accept argument");
case SO_ARG_INVALID_TYPE: return _T("Invalid argument format");
case SO_ARG_MISSING: return _T("Required argument is missing");
case SO_ARG_INVALID_DATA: return _T("Invalid argument data");
default: return _T("Unknown error");
}
}
static void
DoMultiArgs(
CSimpleOpt & args,
int nMultiArgs
)
{
TCHAR ** rgpszArg = NULL;
// get the number of arguments if necessary
if (nMultiArgs == -1) {
// first arg is a count of how many we have
rgpszArg = args.MultiArg(1);
if (!rgpszArg) {
_tprintf(
_T("%s: '%s' (use --help to get command line help)\n"),
GetLastErrorText(args.LastError()), args.OptionText());
return;
}
nMultiArgs = _ttoi(rgpszArg[0]);
}
_tprintf(_T("%s: expecting %d args\n"), args.OptionText(), nMultiArgs);
// get the arguments to follow
rgpszArg = args.MultiArg(nMultiArgs);
if (!rgpszArg) {
_tprintf(
_T("%s: '%s' (use --help to get command line help)\n"),
GetLastErrorText(args.LastError()), args.OptionText());
return;
}
for (int n = 0; n < nMultiArgs; ++n) {
_tprintf(_T("MultiArg %d: %s\n"), n, rgpszArg[n]);
}
}
int _tmain(int argc, TCHAR * argv[]) {
// process the command line to extract that flags for SimpleOpt
int nFlags = SO_O_USEALL;
CSimpleOpt args(argc, argv, g_rgFlags, SO_O_NOERR|SO_O_EXACT);
while (args.Next()) {
nFlags |= args.OptionId();
}
// now process the remainder of the command line with these flags
args.Init(args.FileCount(), args.Files(), g_rgOptions, nFlags);
while (args.Next()) {
if (args.LastError() != SO_SUCCESS) {
_tprintf(
_T("%s: '%s' (use --help to get command line help)\n"),
GetLastErrorText(args.LastError()), args.OptionText());
continue;
}
switch (args.OptionId()) {
case OPT_HELP:
ShowUsage();
return 0;
case OPT_MULTI:
DoMultiArgs(args, -1);
break;
case OPT_MULTI0:
DoMultiArgs(args, 0);
break;
case OPT_MULTI1:
DoMultiArgs(args, 1);
break;
case OPT_MULTI2:
DoMultiArgs(args, 2);
break;
case OPT_STOP:
args.Stop();
break;
default:
if (args.OptionArg()) {
_tprintf(_T("option: %2d, text: '%s', arg: '%s'\n"),
args.OptionId(), args.OptionText(), args.OptionArg());
}
else {
_tprintf(_T("option: %2d, text: '%s'\n"),
args.OptionId(), args.OptionText());
}
}
}
/* process files from command line */
ShowFiles(args.FileCount(), args.Files());
return 0;
}