-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathCommandLineParser.cs
More file actions
187 lines (162 loc) · 7.19 KB
/
CommandLineParser.cs
File metadata and controls
187 lines (162 loc) · 7.19 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
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Linq;
namespace System.CommandLine.Parsing
{
/// <summary>
/// Parses command line input.
/// </summary>
public static class CommandLineParser
{
/// <summary>
/// Parses a list of arguments.
/// </summary>
/// <param name="command">The command to use to parse the command line input.</param>
/// <param name="args">The string array typically passed to a program's <c>Main</c> method.</param>
/// <param name="configuration">The configuration on which the parser's grammar and behaviors are based.</param>
/// <returns>A <see cref="ParseResult"/> providing details about the parse operation.</returns>
public static ParseResult Parse(Command command, IReadOnlyList<string> args, CommandLineConfiguration? configuration = null)
=> Parse(command, args, null, configuration);
/// <summary>
/// Parses a command line string.
/// </summary>
/// <param name="command">The command to use to parse the command line input.</param>
/// <param name="commandLine">The complete command line input prior to splitting and tokenization. This input is not typically available when the parser is called from <c>Program.Main</c>. It is primarily used when calculating completions via the <c>dotnet-suggest</c> tool.</param>
/// <param name="configuration">The configuration on which the parser's grammar and behaviors are based.</param>
/// <remarks>The command line string input will be split into tokens as if it had been passed on the command line.</remarks>
/// <returns>A <see cref="ParseResult"/> providing details about the parse operation.</returns>
public static ParseResult Parse(Command command, string commandLine, CommandLineConfiguration? configuration = null)
=> Parse(command, SplitCommandLine(commandLine).ToArray(), commandLine, configuration);
/// <summary>
/// Splits a string into a sequence of strings based on whitespace and quotation marks.
/// </summary>
/// <param name="commandLine">A command line input string.</param>
/// <returns>A sequence of strings.</returns>
public static IEnumerable<string> SplitCommandLine(string commandLine)
{
var memory = commandLine.AsMemory();
var startTokenIndex = 0;
var pos = 0;
var seeking = Boundary.TokenStart;
var seekingQuote = Boundary.QuoteStart;
while (pos < memory.Length)
{
var c = memory.Span[pos];
if (char.IsWhiteSpace(c))
{
if (seekingQuote == Boundary.QuoteStart)
{
switch (seeking)
{
case Boundary.WordEnd:
yield return CurrentToken();
startTokenIndex = pos;
seeking = Boundary.TokenStart;
break;
case Boundary.TokenStart:
startTokenIndex = pos;
break;
}
}
}
else if (c == '\"')
{
if (seeking == Boundary.TokenStart)
{
switch (seekingQuote)
{
case Boundary.QuoteEnd:
yield return CurrentToken();
startTokenIndex = pos;
seekingQuote = Boundary.QuoteStart;
break;
case Boundary.QuoteStart:
startTokenIndex = pos + 1;
seekingQuote = Boundary.QuoteEnd;
break;
}
}
else
{
switch (seekingQuote)
{
case Boundary.QuoteEnd:
seekingQuote = Boundary.QuoteStart;
break;
case Boundary.QuoteStart:
seekingQuote = Boundary.QuoteEnd;
break;
}
}
}
else if (seeking == Boundary.TokenStart && seekingQuote == Boundary.QuoteStart)
{
seeking = Boundary.WordEnd;
startTokenIndex = pos;
}
Advance();
if (IsAtEndOfInput())
{
switch (seeking)
{
case Boundary.TokenStart:
break;
default:
yield return CurrentToken();
break;
}
}
}
void Advance() => pos++;
string CurrentToken()
{
return memory.Slice(startTokenIndex, IndexOfEndOfToken()).ToString().Replace("\"", "");
}
int IndexOfEndOfToken() => pos - startTokenIndex;
bool IsAtEndOfInput() => pos == memory.Length;
}
private static ParseResult Parse(
Command command,
IReadOnlyList<string> arguments,
string? rawInput,
CommandLineConfiguration? configuration)
{
if (arguments is null)
{
throw new ArgumentNullException(nameof(arguments));
}
using var parseActivity = Activities.ActivitySource.StartActivity(DiagnosticsStrings.ParseMethod);
configuration ??= new CommandLineConfiguration(command);
arguments.Tokenize(
configuration,
inferRootCommand: rawInput is not null,
out List<Token> tokens,
out List<string>? tokenizationErrors);
var operation = new ParseOperation(
tokens,
configuration,
tokenizationErrors,
rawInput);
var result = operation.Parse();
parseActivity?.SetTag(DiagnosticsStrings.Command, result.CommandResult?.Command.Name);
if (result.Errors.Count > 0)
{
parseActivity?.SetStatus(Diagnostics.ActivityStatusCode.Error);
parseActivity?.AddBaggage(DiagnosticsStrings.Errors, string.Join("\n", result.Errors.Select(e => e.Message)));
}
else
{
parseActivity?.SetStatus(Diagnostics.ActivityStatusCode.Ok);
}
return result;
}
private enum Boundary
{
TokenStart,
WordEnd,
QuoteStart,
QuoteEnd
}
}
}