-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlibcOpt.cs
383 lines (340 loc) · 15.2 KB
/
GlibcOpt.cs
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* ngetopt - Unixy command-line option parser library for .NET
* Copyright (c) 2010 Bob Carroll
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ngetopt
{
/// <summary>
/// A .NET implementation of glibc's command-line option parser.
/// </summary>
/// <see cref="http://www.kernel.org/doc/man-pages/online/pages/man3/getopt.3.html"/>
/// <remarks>
/// Read the GETOPT(3) man page for detailed usage instructions.
///
/// GetOpt() and GetOptLong() are both functionally equivalent to getopt()
/// getopt_long(), respectively. GNU extensions are supported with only
/// a few exceptions. POSIXLY_CORRECT is supported both as a property and
/// an environmental variable.
///
/// The Option structure makes use of CLI conventions but is otherwise
/// identical to glibc's option struct. Due to limitations of C#, the Flag
/// member is unused and should always be zero.
///
/// getopt_long_only() is not supported. Using "-W foo" for long options is
/// not supported.
/// </remarks>
/// <example>
/// static int Main(string[] args)
/// {
/// string[] argv = Environment.GetCommandLineArgs();
/// GlibcOpt gopt = new GlibcOpt();
/// int result;
///
/// while ((result = gopt.GetOpt(ref argv, "abc:d::")) != -1) {
///
/// switch (result) {
///
/// case 'a':
/// Console.WriteLine("foo");
/// break;
///
/// case 'b':
/// Console.WriteLine("bar");
/// break;
///
/// case 'c': /* this option requires an argument because a colon follows it in the option string */
/// Console.WriteLine(gopt.OptArgument);
/// break;
///
/// case 'd': /* this option has an optional argument because two colons follow it in the option string */
/// if (gopt.OptArguments == null)
/// Console.WriteLine("baz");
/// else
/// Console.WriteLine(gopt.OptArgument);
/// break;
///
/// default:
/// Console.WriteLine("Usage: helloworld [OPTION]");
/// return 0;
/// }
/// }
///
/// return 0;
/// }
/// </example>
public sealed class GlibcOpt
{
private int _nextchar; /* index of the next character in the argv element */
private string _optarg; /* value of the option argument */
private bool _opterr; /* flag to suppress printing error for unknow options to stderr */
private int _optind; /* next argv index to read (initialised to 1) */
private char _optopt; /* unrecognised option character */
/// <summary>
/// Initialises the option parser.
/// </summary>
public GlibcOpt()
{
_nextchar = 0;
_optarg = null;
_opterr = true;
_optind = 1;
_optopt = '\0';
if (Environment.GetEnvironmentVariable("POSIXLY_CORRECT") != null)
this.POSIXLY_CORRECT = true;
}
/// <summary>
/// Parses the argv array for short command-line options. The first element of
/// argv must be the binary name!
/// </summary>
/// <param name="argv">reference to argv passed into main()</param>
/// <param name="optstring">valid options list</param>
/// <returns>the option character, -1 for no more options, or ? for error</returns>
public int GetOpt(ref string[] argv, string optstring)
{
int longopts = 0;
return GetOptLong(ref argv, optstring, new Option[] { }, ref longopts);
}
/// <summary>
/// Parses the argv array for long command-line options. The first element of
/// argv must be the binary name!
/// </summary>
/// <param name="argv">reference to argv passed into main()</param>
/// <param name="optstring">valid options list</param>
/// <param name="longopts">an array of valid long options</param>
/// <param name="longindex">returns the current long option index</param>
/// <returns>the option character, -1 for no more options, or ? for error</returns>
/// <remarks>For long options only, optstring should be an empty string and not null!</remarks>
public int GetOptLong(ref string[] argv, string optstring, Option[] longopts, ref int longindex)
{
string imgname = Path.GetFileName(argv[0]);
if (!Regex.IsMatch(optstring, "^([+]|-|:)?([A-Za-z0-9]:?:?)*$"))
throw new Exception("Bad option string!");
int res;
Option[] validopts = this.OptStr2LongOpts(optstring);
if (_optind < 1)
_optind = 1;
while (_optind < argv.Length) {
string curitm = argv[_optind];
bool hasarg = false;
if (curitm == "--") { /* -- always ends option scanning */
/* if we're not at the end of the array, then optind points to
* the first non-option in the array */
if (_optind + 1 < argv.Length)
_optind++;
/* end of option scanning */
return -1;
} else if (this.IsOption(curitm)) {
char curchar;
Option curopt;
bool islong = false;
int nextoffset = 1;
if (this.IsOptionLong(curitm)) { /* long options */
/* strip off the leading dashes and argument if there is one */
string optname = curitm.IndexOf('=') > 0 ?
curitm.Substring(2, curitm.IndexOf('=') - 2) :
curitm.Substring(2);
curopt = longopts.Where(o => o.Name == optname).SingleOrDefault();
curchar = curopt.Name != null ? curopt.Val : '\0';
islong = true;
longindex = curopt.Name != null ?
longopts.Select((e, i) => new { Elem = e, Index = i })
.Where(i => ((Option)i.Elem).Equals(curopt)).First().Index :
0;
} else { /* short options */
curchar = curitm.Substring(_nextchar + 1, 1)[0];
curopt = validopts.Where(o => o.Name[0] == curchar).SingleOrDefault();
}
_optopt = '\0';
_optarg = null;
if (curopt.Name != null) {
res = curopt.Val;
hasarg = curopt.HasArg != ArgFlags.None;
/* short option args can be the remainder of the option element or the next argv element
* ex: -ofoo where -o is the option and 'foo' is the argument
* ex: -o foo
*
* long option args can be part of the option element if separated by equals, or the next argv element
* ex: --output=foo where --output is option and 'foo' is the argument
* ex: --output foo
*/
if (!islong && hasarg && curitm.Length > 2 && _nextchar == 0) {
_optarg = curitm.Substring(2);
} else if (!islong && hasarg && curitm.Length == 2 && _optind + 1 < argv.Length && !this.IsOption(argv[_optind + 1])) {
_optarg = argv[_optind + 1];
nextoffset = 2;
} else if (islong && hasarg && curitm.Contains('=')) {
_optarg = curitm.Substring(curitm.IndexOf('=') + 1);
} else if (islong && hasarg && _optind + 1 < argv.Length && !this.IsOption(argv[_optind + 1])) {
_optarg = argv[_optind + 1];
nextoffset = 2;
} else if (hasarg) {
if (curopt.HasArg == ArgFlags.Required) {
if (_opterr)
Console.Error.WriteLine("{0}: option '{1}' requires an argument", imgname, curopt.Name);
res = optstring.StartsWith(":") ? ':' : '?';
_optopt = curopt.Val;
} else {
/* the argument is optional, so no need to complain */
hasarg = false;
}
}
} else {
if (_opterr) {
string optname = (curchar == '\0') ?
curitm :
new string(new char[] { curchar });
Console.Error.WriteLine("{0}: unrecognised option '{1}'", imgname, optname);
}
res = '?';
_optopt = curchar;
}
/* support options provided in the same argv element
* ex: -a -b -c becomes -abc */
if (!islong && !hasarg && _nextchar < curitm.Length - 2)
_nextchar++;
else {
if (hasarg) /* don't permute arguments */
_optind += nextoffset;
else
_optind++;
_nextchar = 0;
}
return res;
} else { /* not an option */
/* strict POSIX mode ends option scanning at the first non-option. + also enables
* this mode. - forces non-options to return as character code 1. */
if (optstring.StartsWith("+") || this.POSIXLY_CORRECT) {
return -1;
} else if (optstring.StartsWith("-")) {
_optind++;
return (char)1;
}
/* deterimine if there are any options left to read... */
bool more = false;
for (int i = _optind; i < argv.Length; i++) {
if (this.IsOption(argv[i])) {
more = true;
break;
}
}
/* if not, we're done */
if (!more) break;
/* permute the argv array throwing the non-option on the end */
Array.Copy(argv, _optind + 1, argv, _optind, argv.Length - _optind - 1);
argv[argv.Length - 1] = curitm;
}
}
/* no more options to read */
return -1;
}
/// <summary>
/// Determines if the given string is an option.
/// </summary>
/// <param name="opt">the string to inspect</param>
/// <returns>true when it's an option, false otherwise</returns>
private bool IsOption(string opt)
{
return opt.StartsWith("-") && opt != "-";
}
/// <summary>
/// Determines if the given string is a long option. Note, this should
/// only be used on known options. Use IsOption() to first deterimine
/// if the string is an option.
/// </summary>
/// <param name="opt">the string to inspect</param>
/// <returns>true when it's an option, false otherwise</returns>
private bool IsOptionLong(string opt)
{
return opt.StartsWith("--") && opt != "--";
}
/// <summary>
/// Parses the valid options list.
/// </summary>
/// <param name="optstring">the options list</param>
/// <returns>an array of valid option structures</returns>
private Option[] OptStr2LongOpts(string optstring)
{
List<Option> optlst = new List<Option>();
for (int i = 0; i < optstring.Length; i++) {
Option curopt = new Option();
curopt.Name = new String(new char[] { optstring[i] });
curopt.Val = optstring[i];
/* one colon means the previous option has an argument, two colons
* mean the option has an _optional_ argument */
if (i + 1 < optstring.Length && optstring[i + 1] == ':') {
if (i + 2 < optstring.Length && optstring[i + 2] == ':') {
curopt.HasArg = ArgFlags.Optional;
i += 2;
} else {
curopt.HasArg = ArgFlags.Required;
i++;
}
}
optlst.Add(curopt);
}
return optlst.ToArray();
}
/// <summary>
/// Index of the next character to read in the current argv element.
/// </summary>
public int NextChar
{
get { return _nextchar; }
}
/// <summary>
/// Index of the next element to read in the argv array.
/// </summary>
public int NextOption
{
get { return _optind; }
set { _optind = value; }
}
/// <summary>
/// Value of the option argument.
/// </summary>
public string OptionArg
{
get { return _optarg; }
}
/// <summary>
/// Unrecognised option character or option character missing
/// an argument.
/// </summary>
public char OptionChar
{
get { return _optopt; }
}
/// <summary>
/// Flag to enable strict POSIX mode.
/// </summary>
public bool POSIXLY_CORRECT { get; set; }
/// <summary>
/// Flag to toggle printing option scanning errors to stderr.
/// </summary>
public bool PrintError
{
get { return _opterr; }
set { _opterr = value; }
}
}
}