1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- #nullable disable
5
-
6
4
using System . CommandLine ;
7
5
using System . CommandLine . Parsing ;
8
6
using System . Diagnostics ;
@@ -17,21 +15,21 @@ namespace Microsoft.DotNet.Cli.Extensions;
17
15
18
16
public static class ParseResultExtensions
19
17
{
20
- ///<summary>
18
+ /// <summary>
21
19
/// Finds the command of the parse result and invokes help for that command.
22
20
/// If no command is specified, invokes help for the application.
23
- ///<summary>
24
- ///<remarks>
21
+ /// <summary>
22
+ /// <remarks>
25
23
/// This is accomplished by finding a set of tokens that should be valid and appending a help token
26
24
/// to that list, then re-parsing the list of tokens. This is not ideal - either we should have a direct way
27
25
/// of invoking help for a ParseResult, or we should eliminate this custom, ad-hoc help invocation by moving
28
26
/// more situations that want to show help into Parsing Errors (which trigger help in the default System.CommandLine pipeline)
29
27
/// or custom Invocation Middleware, so we can more easily create our version of a HelpResult type.
30
- ///</remarks>
28
+ /// </remarks>
31
29
public static void ShowHelp ( this ParseResult parseResult )
32
30
{
33
- // take from the start of the list until we hit an option/--/unparsed token
34
- // since commands can have arguments, we must take those as well in order to get accurate help
31
+ // Take from the start of the list until we hit an option/--/unparsed token.
32
+ // Since commands can have arguments, we must take those as well in order to get accurate help.
35
33
var tokenList = parseResult . Tokens . TakeWhile ( token => token . Type == TokenType . Argument || token . Type == TokenType . Command || token . Type == TokenType . Directive ) . Select ( t => t . Value ) . ToList ( ) ;
36
34
tokenList . Add ( "-h" ) ;
37
35
Instance . Parse ( tokenList ) . Invoke ( ) ;
@@ -57,14 +55,17 @@ public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult)
57
55
}
58
56
}
59
57
60
- ///<summary>Splits a .NET format string by the format placeholders (the {N} parts) to get an array of the literal parts, to be used in message-checking</summary>
58
+ /// <summary>
59
+ /// Splits a .NET format string by the format placeholders (the {N} parts) to get an array of the literal parts, to be used in message-checking.
60
+ /// </summary>
61
61
static string [ ] DistinctFormatStringParts ( string formatString )
62
62
{
63
63
return Regex . Split ( formatString , @"{[0-9]+}" ) ; // match the literal '{', followed by any of 0-9 one or more times, followed by the literal '}'
64
64
}
65
65
66
-
67
- /// <summary>given a string and a series of parts, ensures that all parts are present in the string in sequential order</summary>
66
+ /// <summary>
67
+ /// Given a string and a series of parts, ensures that all parts are present in the string in sequential order.
68
+ /// </summary>
68
69
static bool ErrorContainsAllParts ( ReadOnlySpan < char > error , string [ ] parts )
69
70
{
70
71
foreach ( var part in parts )
@@ -162,19 +163,12 @@ public static bool DiagOptionPrecedesSubcommand(this string[] args, string subCo
162
163
return false ;
163
164
}
164
165
165
- private static string GetSymbolResultValue ( ParseResult parseResult , SymbolResult symbolResult ) => symbolResult switch
166
- {
167
- CommandResult commandResult => commandResult . Command . Name ,
168
- ArgumentResult argResult => argResult . Tokens . FirstOrDefault ( ) ? . Value ?? string . Empty ,
169
- _ => parseResult . GetResult ( DotnetSubCommand ) ? . GetValueOrDefault < string > ( )
170
- } ;
171
-
172
166
public static bool BothArchAndOsOptionsSpecified ( this ParseResult parseResult ) =>
173
167
( parseResult . HasOption ( CommonOptions . ArchitectureOption ) ||
174
168
parseResult . HasOption ( CommonOptions . LongFormArchitectureOption ) ) &&
175
169
parseResult . HasOption ( CommonOptions . OperatingSystemOption ) ;
176
170
177
- internal static string GetCommandLineRuntimeIdentifier ( this ParseResult parseResult )
171
+ internal static string ? GetCommandLineRuntimeIdentifier ( this ParseResult parseResult )
178
172
{
179
173
return parseResult . HasOption ( CommonOptions . RuntimeOptionName ) ?
180
174
parseResult . GetValue < string > ( CommonOptions . RuntimeOptionName ) :
@@ -189,7 +183,7 @@ internal static string GetCommandLineRuntimeIdentifier(this ParseResult parseRes
189
183
190
184
public static bool UsingRunCommandShorthandProjectOption ( this ParseResult parseResult )
191
185
{
192
- if ( parseResult . HasOption ( RunCommandParser . PropertyOption ) && parseResult . GetValue ( RunCommandParser . PropertyOption ) . Any ( ) )
186
+ if ( parseResult . HasOption ( RunCommandParser . PropertyOption ) && ( parseResult . GetValue ( RunCommandParser . PropertyOption ) ? . Any ( ) ?? false ) )
193
187
{
194
188
var projVals = parseResult . GetRunCommandShorthandProjectValues ( ) ;
195
189
if ( projVals . Any ( ) )
@@ -225,7 +219,7 @@ private static IEnumerable<string> GetRunPropertyOptions(ParseResult parseResult
225
219
var propertyValues = propertyOptions . SelectMany ( o => o . Tokens . Select ( t => t . Value ) ) . ToArray ( ) ;
226
220
return propertyValues ;
227
221
228
- static Token GetOptionTokenOrDefault ( SymbolResult symbolResult )
222
+ static Token ? GetOptionTokenOrDefault ( SymbolResult symbolResult )
229
223
{
230
224
if ( symbolResult is not OptionResult optionResult )
231
225
{
@@ -251,7 +245,7 @@ public static void HandleDebugSwitch(this ParseResult parseResult)
251
245
/// If you are inside a command handler or 'normal' System.CommandLine code then you don't need this - the parse error handling
252
246
/// will have covered these cases.
253
247
/// </summary>
254
- public static T SafelyGetValueForOption < T > ( this ParseResult parseResult , Option < T > optionToGet )
248
+ public static T ? SafelyGetValueForOption < T > ( this ParseResult parseResult , Option < T > optionToGet )
255
249
{
256
250
if ( parseResult . GetResult ( optionToGet ) is OptionResult optionResult &&
257
251
! parseResult . Errors . Any ( e => e . SymbolResult == optionResult ) )
0 commit comments