-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathCommandResultExtensions.cs
153 lines (125 loc) · 3.99 KB
/
CommandResultExtensions.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
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.CommandLine.Binding;
using System.CommandLine.Parsing;
namespace System.CommandLine.NamingConventionBinder;
internal static class CommandResultExtensions
{
internal static bool TryGetValueForArgument(
this CommandResult commandResult,
IValueDescriptor valueDescriptor,
out object? value)
{
var arguments = commandResult.Command.Arguments;
for (var i = 0; i < arguments.Count; i++)
{
var argument = arguments[i];
if (valueDescriptor.ValueName.IsMatch(argument.Name))
{
if (commandResult.FindResultFor(argument) is { } argumentResult)
{
value = argumentResult.GetValueOrDefault();
}
else
{
value = valueDescriptor.GetDefaultValue();
}
return true;
}
}
value = null;
return false;
}
internal static bool TryGetValueForOption(
this CommandResult commandResult,
IValueDescriptor valueDescriptor,
out object? value)
{
var options = commandResult.Command.Options;
for (var i = 0; i < options.Count; i++)
{
if (options[i] is Option option)
{
var hasMatchingAlias =
HasMatchingAlias(valueDescriptor, option);
if (hasMatchingAlias)
{
var optionResult = commandResult.FindResultFor(option);
if (optionResult is not null)
{
value = optionResult.GetValueOrDefault();
return true;
}
}
}
}
value = null;
return false;
static bool HasMatchingAlias(
IValueDescriptor valueDescriptor,
Option option)
{
if (option.HasAlias(valueDescriptor.ValueName))
{
return true;
}
foreach (var alias in option.Aliases)
{
if (valueDescriptor.ValueName.IsMatch(alias))
{
return true;
}
}
return false;
}
}
private static bool IsMatch(this string parameterName, string alias)
{
var parameterNameIndex = 0;
var indexAfterPrefix = IndexAfterPrefix(alias);
var parameterCandidateLength = alias.Length - indexAfterPrefix;
for (var aliasIndex = indexAfterPrefix;
aliasIndex < alias.Length && parameterNameIndex < parameterName.Length;
aliasIndex++)
{
var aliasChar = alias[aliasIndex];
if (aliasChar == '-')
{
parameterCandidateLength--;
continue;
}
var parameterNameChar = parameterName[parameterNameIndex];
if (aliasChar == '|')
{
// replacing "|" with "or"
parameterNameIndex += 2;
parameterCandidateLength++;
continue;
}
if (char.ToUpperInvariant(parameterNameChar) != char.ToUpperInvariant(aliasChar))
{
return false;
}
parameterNameIndex++;
}
if (parameterCandidateLength == parameterName.Length)
{
return true;
}
return false;
static int IndexAfterPrefix(string alias)
{
if (alias.Length > 0)
{
switch (alias[0])
{
case '-' when alias.Length > 1 && alias[1] == '-':
return 2;
case '-':
case '/':
return 1;
}
}
return 0;
}
}
}