getopt.net
A port of getopt in pure C#.
Extensions.cs
Go to the documentation of this file.
1using System;
2
3namespace getopt.net {
4
5 using System.Linq;
6 using System.Text;
7
12 public static class Extensions {
13
20 public static Option? FindOptionOrDefault(this Option[] list, string optName) {
21 if (string.IsNullOrEmpty(optName)) { throw new ArgumentNullException(nameof(optName), "optName must not be null!"); }
22
23 return list.FirstOrDefault(o => o.Name?.Equals(optName, StringComparison.InvariantCulture) == true);
24 }
25
32 public static Option? FindOptionOrDefault(this Option[] list, char optVal) => list.FirstOrDefault(o => o.Value == optVal);
33
39 public static string? ToShortOptString(this Option[] list) {
40 if (list is null || list.Length == 0) { return null; }
41
42 var sBuilder = new StringBuilder();
43
44 foreach (var opt in list) {
45 sBuilder.Append((char)opt.Value);
46 switch (opt.ArgumentType) {
47 case ArgumentType.Required:
48 sBuilder.Append(':');
49 break;
50 case ArgumentType.Optional:
51 sBuilder.Append(';');
52 break;
53 default: break;
54 }
55 }
56
57 return sBuilder.ToString();
58 }
59
60 }
61}
62
static ? string ToShortOptString(this Option[] list)
Creates a short opt string from an array of Option objects.
Definition: Extensions.cs:39
static ? Option FindOptionOrDefault(this Option[] list, char optVal)
Finds an option in the list list with the Option.Value optVal .
static ? Option FindOptionOrDefault(this Option[] list, string optName)
Finds an option with the name optName
Definition: Extensions.cs:20
ArgumentType
Enumeration containing the argument types possible for getopt.
Definition: ArgumentType.cs:8
Represents a single long option for getopt.
Definition: Option.cs:8