-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeyes.GetOpt.cs
359 lines (312 loc) · 9.16 KB
/
Heyes.GetOpt.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
using System;
using System.Collections;
namespace Heyes
{
/// <summary>
/// The GetOpt class allows you to easily define and handle
/// command line options and arguments. Various styles of
/// options are catered for each optionally having its own
/// argument. For example: long options: (--foo), long options
/// with arguments (--foo=bar), short options (-f, /f), short
/// options with arguments (-f bar, -fbar) and consolidated
/// short options (-fb).
/// </summary>
/// <example>
/// This example defines 4 options (a, b, foo and bar). "foo"
/// is required to be given with an argument (eg. --foo=xyz)
/// and bar can be supplied with or without an argument.
/// <code>
/// try {
/// GetOpt foo = new GetOpt(args);
/// GetOpt.SetOpts(new string[] {"a", "b", "foo=", "bar=?"})
/// GetOpt.Parse();
/// } catch (ArgumentException e) {
/// myProgram.PrintUsage();
/// }
/// </code>
/// </example>
public class GetOpt
{
#region Internal structure
/// <summary>
/// The CommandlineOption struct is used for representing
/// a single option, with or without its argument.
/// </summary>
internal struct CommandlineOption
{
internal string option;
internal string arg;
internal bool argPresent;
internal bool argRequired;
internal bool argOptional;
}
#endregion
#region Class fields
private bool parsed;
private string[] args;
private ArrayList givenArgs;
private Hashtable givenOpts;
private Hashtable shortOpts;
private Hashtable longOpts;
#endregion
#region Class properties
/// <summary>
/// Returns an ArrayList of supplied arguments (not options).
/// If no arguments are supplied (ie none option args) then this
/// will be empty.
/// </summary>
public ArrayList Args
{
get
{
return givenArgs;
}
}
#endregion
#region Class methods
/// <summary>
/// Constructor for the GetOpt class. Pass it the programs args
/// list.
/// </summary>
/// <param name="args">The args as supplied to the Main() method.</param>
public GetOpt(string[] args)
{
this.args = args;
this.parsed = false;
this.givenArgs = new ArrayList();
this.givenOpts = new Hashtable();
this.shortOpts = new Hashtable();
this.longOpts = new Hashtable();
}
/// <summary>
/// Defines the options you require for your program. You should
/// pass if the options as a string array. There are two modifiers
/// you can use to specify option arguments. Use "=" at the end of
/// your option to specify that this option has to have an argument,
/// and use =? at the end of your option to specify that an argument
/// is optional.
/// </summary>
/// <example>
/// <code>
/// GetOpt.SetOpts(new string[] {"a", "b", "foo=", "bar=?"})
/// </code>
/// </example>
/// <param name="options">
/// A string array of the options without the preceding
/// "-", "--" or "/".
/// </param>
public void SetOpts(string[] options)
{
foreach (string option in options)
{
CommandlineOption optionStruct = new CommandlineOption();
// Argument required
if (option.EndsWith("="))
{
optionStruct.option = option.Substring(0, option.Length - 1);
optionStruct.argRequired = true;
// Argument optional
}
else if (option.EndsWith("=?"))
{
optionStruct.option = option.Substring(0, option.Length - 2);
optionStruct.argOptional = true;
// No argument
}
else
{
optionStruct.option = option;
}
if (optionStruct.option.Length == 1)
{
this.shortOpts.Add(optionStruct.option, optionStruct);
}
else
{
this.longOpts.Add(optionStruct.option, optionStruct);
}
}
}
/// <summary>
/// Initiates the parsing of the options/arguments supplied
/// to the program.
/// </summary>
/// <exception cref="System.ArgumentException">
/// Thrown when: 1) an option is supplied with an argument and
/// it shouldn't be, 2) an option is required to have an argument
/// and it isn't supplied with one and 3) an option is supplied
/// that has not been defined.
/// </exception>
public void Parse()
{
this.parsed = true;
bool allowOptions = true;
for (int i = 0; i < this.args.Length; ++i)
{
CommandlineOption optionStruct = new CommandlineOption();
int idx;
string arg = this.args[i];
// no more options
if (arg == "--")
{
allowOptions = false;
// Long option
}
else if (arg.StartsWith("--") && allowOptions)
{
// Option has an argument (eg --foo=bar)
if ((idx = arg.IndexOf("=")) != -1)
{
optionStruct.option = arg.Substring(2, idx - 2);
optionStruct.arg = arg.Substring(idx + 1);
optionStruct.argPresent = true;
// Option doesn't have an argument
}
else
{
optionStruct.option = arg.Substring(2);
}
// Checks
if (!this.longOpts.ContainsKey(optionStruct.option))
{
throw new ArgumentException("Unknown option specified", optionStruct.option);
}
else if (((CommandlineOption)this.longOpts[optionStruct.option]).argRequired && optionStruct.argPresent == false)
{
throw new ArgumentException("Option requires an argument", optionStruct.option);
}
else if (((CommandlineOption)this.longOpts[optionStruct.option]).argOptional == false
&& ((CommandlineOption)this.longOpts[optionStruct.option]).argRequired == false
&& optionStruct.argPresent == true)
{
throw new ArgumentException("No argument permitted for this option", optionStruct.option);
}
// Add to defined options hashtable
this.givenOpts.Add(optionStruct.option, optionStruct);
// Short option(s)
}
else if ((arg.StartsWith("-") || arg.StartsWith("/")) && allowOptions)
{
optionStruct.option = arg.Substring(1, 1);
// Is this option defined?
if (this.shortOpts.ContainsKey(optionStruct.option))
{
CommandlineOption definedOption = (CommandlineOption)this.shortOpts[optionStruct.option];
// Check for arguments to the option
if (definedOption.argOptional || definedOption.argRequired)
{
if (arg.Length > 2)
{
optionStruct.argPresent = true;
optionStruct.arg = arg.Substring(2);
}
else if ((i + 1) < args.Length)
{
optionStruct.argPresent = true;
optionStruct.arg = this.args[++i];
// No option, but one was required
}
else if (definedOption.argRequired)
{
throw new ArgumentException("Option requires an argument", optionStruct.option);
}
// No argument optional or required, so if arg is more
// than just one letter, it could be multiple consolidated
// options.
}
else if (arg.Length > 2)
{
this.args[i] = "-" + arg.Substring(2);
--i;
}
// Add to defined options hashtable
this.givenOpts.Add(optionStruct.option, optionStruct);
}
else
{
throw new ArgumentException("Unknown option specified", optionStruct.option);
}
// Not an option, an argument
}
else
{
this.givenArgs.Add(arg);
}
}
}
/// <summary>
/// Returns true/false as to whether an option has been supplied.
/// </summary>
/// <param name="option">The option in question</param>
/// <returns>Whether the option was given</returns>
/// <exception cref="System.Exception">
/// Thrown when the Parse() method has not been called.
/// </exception>
public bool IsDefined(string option)
{
if (this.parsed)
{
return this.givenOpts.ContainsKey(option);
}
else
{
throw new Exception("Call the Parse() method first");
}
}
/// <summary>
/// Returns true/false based on whether the given option has
/// an argument or not.
/// </summary>
/// <param name="option">The option to check</param>
/// <returns>Whether the option has an argument</returns>
/// <example>
/// <code>
/// if (optionParser.IsDefined("bar") && optionParser.HasArgument("bar")) {
/// ...
/// }
/// </code>
/// </example>
/// <exception cref="System.Exception">
/// Thrown when the given option is not defined
/// </exception>
public bool HasArgument(string option)
{
if (this.IsDefined(option))
{
return ((CommandlineOption)this.givenOpts[option]).argPresent;
}
else
{
throw new ArgumentNullException("Option ({0}) is not defined");
}
}
/// <summary>
/// Returns the supplied options argument. Throws an exception
/// if option does not have one.
/// </summary>
/// <param name="option">The option you want the argument for</param>
/// <returns>The options argument</returns>
/// <example>
/// <code>
/// if (optionParser.IsDefined("bar") && optionParser.HasArgument("bar")) {
/// string optionArg = optionParser.GetOptionArg("bar");
/// }
/// </code>
/// </example>
/// <exception cref="System.Exception">
/// Thrown when the supplied option does not have an argument
/// </exception>
public string GetOptionArg(string option)
{
if (this.HasArgument(option))
{
return ((CommandlineOption)this.givenOpts[option]).arg;
}
else
{
throw new Exception("Option does not have an argument");
}
}
#endregion
}
}