-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandApplication.cs
647 lines (572 loc) · 20.1 KB
/
CommandApplication.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace IS4.SFI.Application
{
/// <summary>
/// An abstract command-based application.
/// </summary>
public abstract class CommandApplication
{
/// <summary>
/// The assembly of the current program.
/// </summary>
protected Assembly Assembly => Assembly.GetExecutingAssembly();
/// <summary>
/// The name of the application.
/// </summary>
protected virtual string ApplicationName => Assembly.GetName().Name;
/// <summary>
/// The name of the executable.
/// </summary>
protected virtual string ExecutableName => Path.GetFileNameWithoutExtension(Assembly.Location);
/// <summary>
/// The writer to use for writing messages for the user.
/// </summary>
protected abstract TextWriter LogWriter { get; }
/// <summary>
/// The expected window width.
/// </summary>
protected abstract int WindowWidth { get; }
/// <summary>
/// Writes a short text about the application.
/// </summary>
public virtual void Banner()
{
var asm = Assembly;
string msg = "";
var copyright = asm.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
if(copyright.Length > 0)
{
msg = ((AssemblyCopyrightAttribute)copyright[0]).Copyright;
}
var title = asm.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if(title.Length > 0)
{
msg += " "+((AssemblyTitleAttribute)title[0]).Title;
var version = asm.GetName().Version;
if(version != null)
{
if(version.Build == 0)
{
version = new Version(version.Major, version.Minor);
}else{
version = new Version(version.Major, version.Minor, version.Build);
}
msg += " v"+version.ToString();
}
}
var author = asm.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if(author.Length > 0)
{
msg += " by "+((AssemblyCompanyAttribute)author[0]).Company;
}
LogWriter.WriteLine(msg);
}
/// <summary>
/// Writes the description of the application.
/// </summary>
public virtual void Description()
{
foreach(AssemblyDescriptionAttribute desc in Assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false))
{
LogWriter.WriteLine(desc.Description);
}
}
/// <summary>
/// Returns the list of available options, for example
/// as <see cref="OptionInfoCollection"/>.
/// </summary>
/// <returns>The list of options available to use.</returns>
public virtual IList<OptionInfo> GetOptions()
{
return new OptionInfoCollection();
}
/// <summary>
/// The command usage of the application.
/// </summary>
protected virtual string Usage => "";
/// <summary>
/// Writes the help screen and terminates the application.
/// </summary>
/// <exception cref="ApplicationExitException">
/// Thrown at the end of the method.
/// </exception>
protected void Help()
{
Banner();
Description();
LogWriter.WriteLine();
if(String.IsNullOrWhiteSpace(ExecutableName))
{
LogWriter.WriteLine("Usage: {0}", Usage);
}else{
LogWriter.WriteLine("Usage: {0} {1}", ExecutableName, Usage);
}
LogWriter.WriteLine();
const string optFormat = " -{0} [ --{1} ] {2} ";
const string optFormatLong = " --{1} {2} ";
var options = GetOptions();
int colLength = options.Max(o => String.Format(o.ShortName == null ? optFormatLong : optFormat, o.ShortName, o.LongName, o.ArgumentText).Length);
foreach(var opt in options)
{
string usage = String.Format(opt.ShortName == null ? optFormatLong : optFormat, opt.ShortName, opt.LongName, opt.ArgumentText);
LogWriter.Write(usage);
LogWriter.Write(new string(' ', colLength-usage.Length));
OutputWrapPad(opt.Description, colLength, WindowWidth - colLength);
}
Notes();
throw new ApplicationExitException();
}
/// <summary>
/// Writes additional notes about the application or its usage.
/// </summary>
protected virtual void Notes()
{
LogWriter.WriteLine();
LogWriter.WriteLine("Examples:");
Args("describe dir/* out.ttl");
Desc("Describes all files in 'dir' using the default components, and saves the RDF output to 'out.ttl'.");
Args("describe -d -h sha1 dir out.ttl");
Desc("Same as above, but only loads the files in the directory as data ('-d'), without storing their names or other metadata.");
Desc("In addition to that, the SHA-1 hash algorithm is used to produce 'ni:' URIs for content.");
Args("describe -f rdf dir -");
Desc("As above, but writes the RDF description as RDF/XML to the standard output.");
Args("describe -b -f jsonld dir -");
Desc("Writes the RDF description in JSON-LD instead. This requires buffering the output ('-b').");
Args("describe -r urn:uuid: dir -");
Desc("Does not use blank nodes to identify entities, instead using URIs starting with 'urn:uuid:'.");
Args("describe -x *-hash:* -i data-hash:sha1 dir -");
Desc("Does not use any of the supported hash algorithms, with the exception of SHA-1, to describe data.");
Args("list -x *-format:* -i *-format:image/*");
Desc("Excludes all file formats from the list of components, but keeps specific image formats.");
Args("list -x * -i analyzer:stream-factory -i analyzer:data-object");
Desc("Only allows the analysis of actual data, not files.");
Args("list --analyzer:stream-factory:max-depth-for-formats \"\"");
Desc("Sets this property value to null, disabling depth checks.");
void Args(string str)
{
LogWriter.WriteLine();
LogWriter.WriteLine(str);
}
void Desc(string str)
{
LogWriter.Write(" ");
OutputWrapPad(str, 2, WindowWidth - 2);
}
}
/// <summary>
/// Writes text to the output, padded by <paramref name="pad"/> spaces
/// from both sides of the window, wrapping it if necessary.
/// </summary>
/// <param name="text">The text to write.</param>
/// <param name="pad">The number of spaces to pad with.</param>
public void OutputWrapPad(string text, int pad)
{
OutputWrapPad(text, pad, WindowWidth - pad);
}
/// <summary>
/// Writes text to the output, padded by <paramref name="padLeft"/> spaces
/// from both sides of the window, wrapping it if necessary.
/// </summary>
/// <param name="text">The text to write.</param>
/// <param name="padLeft">The number of spaces to pad with.</param>
/// <param name="textWidth">The maximum characters allowed on a line.</param>
public void OutputWrapPad(string text, int padLeft, int textWidth)
{
int totalLength = 0;
foreach(string s in text.Split(' '))
{
bool first = totalLength == 0;
totalLength += s.Length;
if(totalLength >= textWidth-1)
{
LogWriter.WriteLine();
if(padLeft > 0)
{
LogWriter.Write(new string(' ', padLeft));
}
totalLength = s.Length;
first = true;
}
if(!first)
{
LogWriter.Write(" ");
totalLength += 1;
}
LogWriter.Write(s);
}
if(totalLength > 0)
{
LogWriter.WriteLine();
}
}
/// <summary>
/// Called internally from <see cref="Parse(string[])"/> when
/// an option is found.
/// </summary>
/// <param name="option">The name of the option, without any delimiter characters.</param>
/// <returns>
/// One of the values of <see cref="OptionArgumentFlags"/> specifying
/// the argument handling for this option.
/// </returns>
protected abstract OptionArgumentFlags OnOptionFound(string option);
/// <summary>
/// Calls <see cref="OnOptionFound(string)"/> when
/// an option is found.
/// </summary>
/// <param name="option">The name of the option, without any delimiter characters.</param>
/// <returns>
/// One of the values of <see cref="OptionArgumentFlags"/> specifying
/// the argument handling for this option.
/// </returns>
protected virtual OptionArgumentFlags OptionFound(string option)
{
return OnOptionFound(option);
}
/// <summary>
/// Called internally from <see cref="Parse(string[])"/> when
/// an argument for an option is found.
/// </summary>
/// <param name="option">The name of the option, without any delimiter characters.</param>
/// <param name="argument">The argument of the option.</param>
/// <param name="flags">The argument handlings flags previously returned by <see cref="OnOperandFound(string)"/>.</param>
protected abstract void OnOptionArgumentFound(string option, string? argument, OptionArgumentFlags flags);
/// <summary>
/// Calls <see cref="OnOptionArgumentFound(string, string?, OptionArgumentFlags)"/>
/// when an argument for an option is found.
/// </summary>
/// <param name="option">The name of the option, without any delimiter characters.</param>
/// <param name="argument">The argument of the option.</param>
/// <param name="flags">The argument handlings flags previously returned by <see cref="OnOperandFound(string)"/>.</param>
protected virtual void OptionArgumentFound(string option, string? argument, OptionArgumentFlags flags)
{
OnOptionArgumentFound(option, argument, flags);
}
/// <summary>
/// Called internally from <see cref="Parse(string[])"/> when
/// the command's operand is found.
/// </summary>
/// <param name="operand">The value of the operand.</param>
/// <returns>
/// One of the values of <see cref="OperandState"/> specifying
/// the state of the parser after this operand.
/// </returns>
protected abstract OperandState OnOperandFound(string operand);
/// <summary>
/// Modifies the input argument in a desirable way
/// before it is parsed by <see cref="Parse(string[])"/>.
/// </summary>
/// <param name="arg">The input argument.</param>
/// <returns>The modified value.</returns>
protected virtual string ProcessArg(string arg)
{
return arg;
}
/// <summary>
/// Obtains the canonical representation of an option.
/// </summary>
/// <param name="option">The option name.</param>
/// <returns>The canonical option name for <paramref name="option"/>.</returns>
protected virtual string GetCanonicalOption(string option)
{
return option.Length <= 1 ? option : option.ToLowerInvariant();
}
/// <summary>
/// Parses the arguments provided to the application
/// and initializes it with the values specified
/// by the arguments.
/// </summary>
/// <param name="args">The arguments to the application.</param>
/// <exception cref="ApplicationExitException">
/// Could be thrown from one of the option or operand handler
/// to indicate that the application should be terminated.
/// </exception>
public void Parse(string[] args)
{
var added = new HashSet<string>();
bool operands = false;
for(int i = 0; i < args.Length; i++)
{
string arg = ProcessArg(args[i]);
if(operands)
{
OnOperandFound(arg);
}else if(arg == "--")
{
operands = true;
}else if(arg.StartsWith("--"))
{
OptionArgumentFlags flags;
string name;
string? argument;
int delim = arg.IndexOf('=');
if(delim != -1)
{
name = arg.Substring(2, delim - 2);
flags = OptionFound(name);
if((flags & OptionArgumentFlags.HasArgument) == 0)
{
throw ArgumentNotExpected(name);
}
argument = arg.Substring(delim + 1);
}else{
name = arg.Substring(2);
flags = OptionFound(name);
if((flags & OptionArgumentFlags.HasArgument) == 0)
{
CheckAdded(name, flags);
continue;
}
if((flags & OptionArgumentFlags.RequiredArgument) == OptionArgumentFlags.RequiredArgument)
{
if(++i >= args.Length) throw ArgumentExpected(name);
argument = ProcessArg(args[i]);
}else{
argument = null;
}
}
CheckAdded(name, flags);
OptionArgumentFound(name, argument, flags);
}else if(arg.Length > 1 && arg[0] == '-' && IsOptionChar(arg[1]))
{
for(int j = 1; j < arg.Length; j++)
{
string name = arg[j].ToString();
string? argument = String.Join("", arg.Skip(j+1).TakeWhile(c => !IsOptionChar(c)));
var flags = OptionFound(name);
CheckAdded(name, flags);
if((flags & OptionArgumentFlags.HasArgument) == 0)
{
if(argument.Length > 0)
{
throw ArgumentNotExpected(name);
}
continue;
}
if((flags & OptionArgumentFlags.RequiredArgument) == OptionArgumentFlags.RequiredArgument)
{
if(argument.Length == 0)
{
if(j+1 < arg.Length)
{
argument = arg.Substring(j+1);
j = arg.Length-1;
}else{
if(++i >= args.Length) throw ArgumentExpected(name);
argument = ProcessArg(args[i]);
argument = ProcessArg(args[i]);
}
}
}else{
if(argument.Length == 0)
{
argument = null;
}
}
OptionArgumentFound(name, argument, flags);
j += argument?.Length ?? 0;
}
}else{
if(OnOperandFound(arg) == OperandState.OnlyOperands)
{
operands = true;
}
}
}
void CheckAdded(string name, OptionArgumentFlags flags)
{
if((flags & OptionArgumentFlags.AllowMultiple) == 0)
{
if(!added!.Add(GetCanonicalOption(name)))
{
throw OptionAlreadySpecified(name);
}
}
}
}
private static bool IsOptionChar(char c)
{
return c == '?' || Char.IsLetter(c);
}
/// <summary>
/// Produces an exception when an unrecognized option is found.
/// </summary>
/// <param name="option">The name of the option.</param>
/// <returns>The exception for this situation.</returns>
public ApplicationException UnrecognizedOption(string option)
{
return new ApplicationException("Unrecognized option '"+option+"'.");
}
/// <summary>
/// Produces an exception when an option should
/// have an argument, but none is found in the input.
/// </summary>
/// <param name="option">The name of the option.</param>
/// <returns>The exception for this situation.</returns>
public ApplicationException ArgumentExpected(string option)
{
return new ApplicationException("Argument expected for option '"+option+"'.");
}
/// <summary>
/// Produces an exception when an option should not
/// have an argument, but one is assigned to it.
/// </summary>
/// <param name="option">The name of the option.</param>
/// <returns>The exception for this situation.</returns>
public ApplicationException ArgumentNotExpected(string option)
{
return new ApplicationException("Argument not expected for option '"+option+"'.");
}
/// <summary>
/// Produces an exception when an option has
/// an argument in an invalid form.
/// </summary>
/// <param name="option">The name of the option.</param>
/// <param name="expected">The expected form of the argument.</param>
/// <returns>The exception for this situation.</returns>
public ApplicationException ArgumentInvalid(string option, string expected)
{
return new ApplicationException("Invalid argument provided for option '"+option+"', "+expected+" expected.");
}
/// <summary>
/// Produces an exception when an option should
/// be specified only once, but it was used multiple times.
/// </summary>
/// <param name="option">The name of the option.</param>
/// <returns>The exception for this situation.</returns>
public ApplicationException OptionAlreadySpecified(string option)
{
return new ApplicationException("Option '"+option+"' has been already specified.");
}
/// <summary>
/// Provides a collection of instances of <see cref="OptionInfo"/>
/// with a convenient <see cref="Add(string, string, string, string)"/>
/// method.
/// </summary>
public class OptionInfoCollection : List<OptionInfo>
{
/// <summary>
/// Creates a new instance of the collection.
/// </summary>
public OptionInfoCollection()
{
}
/// <summary>
/// Creates a new instance of the collection with a given capacity.
/// </summary>
/// <param name="capacity">The capacity of the collection.</param>
public OptionInfoCollection(int capacity) : base(capacity)
{
}
/// <summary>
/// Creates a new instance of the collection from existing members.
/// </summary>
/// <param name="collection">The members of the collection.</param>
public OptionInfoCollection(IEnumerable<OptionInfo> collection) : base(collection)
{
}
/// <summary>
/// Adds a new option to the collection.
/// </summary>
/// <param name="shortName">The short name of the option.</param>
/// <param name="longName">The long name of the option.</param>
/// <param name="argument">The type of the argument for the option.</param>
/// <param name="description">The description of the option.</param>
public void Add(string? shortName, string longName, string? argument, string description)
{
Add(new OptionInfo(shortName, longName, argument, description));
}
}
/// <summary>
/// Represents a single option.
/// </summary>
public readonly struct OptionInfo
{
/// <summary>
/// The short name of the option.
/// </summary>
public string? ShortName { get; }
/// <summary>
/// The long name of the option.
/// </summary>
public string LongName { get; }
/// <summary>
/// The type of the argument for the option.
/// </summary>
public string? Argument { get; }
/// <summary>
/// The description of the option.
/// </summary>
public string Description { get; }
/// <summary>
/// A text when the argument should be displayed.
/// </summary>
public string ArgumentText => Argument == null ? "" : Argument+" ";
/// <summary>
/// Creates a new instance of the option.
/// </summary>
/// <param name="shortName">The value of <see cref="ShortName"/>.</param>
/// <param name="longName">The value of <see cref="LongName"/>.</param>
/// <param name="argument">The value of <see cref="Argument"/>.</param>
/// <param name="description">The value of <see cref="Description"/>.</param>
public OptionInfo(string? shortName, string longName, string? argument, string description) : this()
{
ShortName = shortName;
LongName = longName;
Argument = argument;
Description = description;
}
}
/// <summary>
/// Thrown from within one of the methods to indicate that the
/// application should be terminated.
/// </summary>
public class ApplicationExitException : Exception
{
}
}
/// <summary>
/// Specifies the argument handling for an encountered option.
/// </summary>
[Flags]
public enum OptionArgumentFlags
{
/// <summary>
/// The option should not have any argument.
/// </summary>
None = 0,
/// <summary>
/// The option has an argument.
/// </summary>
HasArgument = 0b10,
/// <summary>
/// The option has an optional argument.
/// </summary>
OptionalArgument = 0b10,
/// <summary>
/// The option has a required argument.
/// </summary>
RequiredArgument = 0b110,
/// <summary>
/// The option permits multiple values.
/// </summary>
AllowMultiple = 0b1000
}
/// <summary>
/// Specifies the state of the parser after an operand is encountered.
/// </summary>
public enum OperandState
{
/// <summary>
/// The options may still be provided.
/// </summary>
ContinueOptions,
/// <summary>
/// Only operands are accepted after this point.
/// </summary>
OnlyOperands
}
}