-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathOption.cs
More file actions
120 lines (113 loc) · 4.82 KB
/
Option.cs
File metadata and controls
120 lines (113 loc) · 4.82 KB
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
using BytecodeApi.Extensions;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace BytecodeApi.CommandLineParser;
/// <summary>
/// Represents a commandline option, specified by possible arguments and alternatives.
/// <para>Example: "-p 12345" and "--password 12345"</para>
/// </summary>
[DebuggerDisplay($"{nameof(Option)}: Arguments = {{string.Join(\"|\", Arguments)}}, Alternatives = {{string.Join(\"|\", Alternatives)}}")]
public sealed class Option : IEquatable<Option>
{
/// <summary>
/// Gets a collection of strings that defines what arguments apply to this commandline option.
/// </summary>
public ReadOnlyCollection<string> Arguments { get; }
/// <summary>
/// Gets a collection of strings that defines what arguments apply to this commandline option alternatively.
/// </summary>
public ReadOnlyCollection<string> Alternatives { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Option" /> class with the specified collection of arguments.
/// </summary>
/// <param name="arguments">A collection of strings that defines what arguments apply to this commandline option.</param>
public Option(params string[] arguments) : this(arguments, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Option" /> class with the specified collection of arguments and alternatives.
/// </summary>
/// <param name="arguments">A collection of strings that defines what arguments apply to this commandline option.</param>
/// <param name="alternatives">A collection of strings that defines what arguments apply to this commandline option alternatively.</param>
public Option(string[] arguments, string[]? alternatives)
{
Check.ArgumentNull(arguments);
Check.ArgumentEx.ArrayElementsRequired(arguments);
Check.ArgumentEx.ArrayValuesNotNull(arguments);
Check.ArgumentEx.ArrayValuesNotStringEmpty(arguments);
Check.Argument(arguments.All(item => Validate.AlphaNumeric(item) || item == "?"), nameof(arguments), "String must be alphanumeric or a single question mark.");
if (alternatives != null)
{
Check.ArgumentEx.ArrayElementsRequired(alternatives);
Check.ArgumentEx.ArrayValuesNotNull(alternatives);
Check.ArgumentEx.ArrayValuesNotStringEmpty(alternatives);
Check.Argument(alternatives.All(Validate.AlphaNumeric), nameof(alternatives), "String must be alphanumeric.");
}
Arguments = arguments.ToReadOnlyCollection();
Alternatives = (alternatives ?? []).ToReadOnlyCollection();
}
/// <summary>
/// Determines whether the specified <see cref="object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// <see langword="true" />, if the specified <see cref="object" /> is equal to this instance;
/// otherwise, <see langword="false" />.
/// </returns>
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is Option option && Equals(option);
}
/// <summary>
/// Determines whether this instance is equal to another <see cref="Option" />.
/// </summary>
/// <param name="other">The <see cref="Option" /> to compare to this instance.</param>
/// <returns>
/// <see langword="true" />, if this instance is equal to the <paramref name="other" /> parameter;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Equals([NotNullWhen(true)] Option? other)
{
return
other != null &&
CSharp.TypeEquals(this, other) &&
Arguments.SequenceEqual(other.Arguments) &&
Alternatives.SequenceEqual(other.Alternatives);
}
/// <summary>
/// Returns a hash code for this <see cref="Option" />.
/// </summary>
/// <returns>
/// The hash code for this <see cref="Option" /> instance.
/// </returns>
public override int GetHashCode()
{
return HashCode.Combine(Arguments.Concat(Alternatives).ToArray());
}
/// <summary>
/// Compares two <see cref="Option" /> instances for equality.
/// </summary>
/// <param name="a">The first <see cref="Option" /> to compare.</param>
/// <param name="b">The second <see cref="Option" /> to compare.</param>
/// <returns>
/// <see langword="true" />, if both <see cref="Option" /> are equal;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator ==(Option? a, Option? b)
{
return Equals(a, b);
}
/// <summary>
/// Compares two <see cref="Option" /> instances for inequality.
/// </summary>
/// <param name="a">The first <see cref="Option" /> to compare.</param>
/// <param name="b">The second <see cref="Option" /> to compare.</param>
/// <returns>
/// <see langword="true" />, if both <see cref="Option" /> are not equal;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator !=(Option? a, Option? b)
{
return !(a == b);
}
}