11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4- #nullable disable
5-
64using System . CommandLine ;
75using System . CommandLine . Parsing ;
86using System . Diagnostics ;
@@ -17,21 +15,21 @@ namespace Microsoft.DotNet.Cli.Extensions;
1715
1816public static class ParseResultExtensions
1917{
20- ///<summary>
18+ /// <summary>
2119 /// Finds the command of the parse result and invokes help for that command.
2220 /// If no command is specified, invokes help for the application.
23- ///<summary>
24- ///<remarks>
21+ /// <summary>
22+ /// <remarks>
2523 /// This is accomplished by finding a set of tokens that should be valid and appending a help token
2624 /// to that list, then re-parsing the list of tokens. This is not ideal - either we should have a direct way
2725 /// of invoking help for a ParseResult, or we should eliminate this custom, ad-hoc help invocation by moving
2826 /// more situations that want to show help into Parsing Errors (which trigger help in the default System.CommandLine pipeline)
2927 /// or custom Invocation Middleware, so we can more easily create our version of a HelpResult type.
30- ///</remarks>
28+ /// </remarks>
3129 public static void ShowHelp ( this ParseResult parseResult )
3230 {
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.
3533 var tokenList = parseResult . Tokens . TakeWhile ( token => token . Type == TokenType . Argument || token . Type == TokenType . Command || token . Type == TokenType . Directive ) . Select ( t => t . Value ) . ToList ( ) ;
3634 tokenList . Add ( "-h" ) ;
3735 Instance . Parse ( tokenList ) . Invoke ( ) ;
@@ -57,14 +55,17 @@ public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult)
5755 }
5856 }
5957
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>
6161 static string [ ] DistinctFormatStringParts ( string formatString )
6262 {
6363 return Regex . Split ( formatString , @"{[0-9]+}" ) ; // match the literal '{', followed by any of 0-9 one or more times, followed by the literal '}'
6464 }
6565
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>
6869 static bool ErrorContainsAllParts ( ReadOnlySpan < char > error , string [ ] parts )
6970 {
7071 foreach ( var part in parts )
@@ -162,19 +163,12 @@ public static bool DiagOptionPrecedesSubcommand(this string[] args, string subCo
162163 return false ;
163164 }
164165
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-
172166 public static bool BothArchAndOsOptionsSpecified ( this ParseResult parseResult ) =>
173167 ( parseResult . HasOption ( CommonOptions . ArchitectureOption ) ||
174168 parseResult . HasOption ( CommonOptions . LongFormArchitectureOption ) ) &&
175169 parseResult . HasOption ( CommonOptions . OperatingSystemOption ) ;
176170
177- internal static string GetCommandLineRuntimeIdentifier ( this ParseResult parseResult )
171+ internal static string ? GetCommandLineRuntimeIdentifier ( this ParseResult parseResult )
178172 {
179173 return parseResult . HasOption ( CommonOptions . RuntimeOptionName ) ?
180174 parseResult . GetValue < string > ( CommonOptions . RuntimeOptionName ) :
@@ -189,7 +183,7 @@ internal static string GetCommandLineRuntimeIdentifier(this ParseResult parseRes
189183
190184 public static bool UsingRunCommandShorthandProjectOption ( this ParseResult parseResult )
191185 {
192- if ( parseResult . HasOption ( RunCommandParser . PropertyOption ) && parseResult . GetValue ( RunCommandParser . PropertyOption ) . Any ( ) )
186+ if ( parseResult . HasOption ( RunCommandParser . PropertyOption ) && ( parseResult . GetValue ( RunCommandParser . PropertyOption ) ? . Any ( ) ?? false ) )
193187 {
194188 var projVals = parseResult . GetRunCommandShorthandProjectValues ( ) ;
195189 if ( projVals . Any ( ) )
@@ -225,7 +219,7 @@ private static IEnumerable<string> GetRunPropertyOptions(ParseResult parseResult
225219 var propertyValues = propertyOptions . SelectMany ( o => o . Tokens . Select ( t => t . Value ) ) . ToArray ( ) ;
226220 return propertyValues ;
227221
228- static Token GetOptionTokenOrDefault ( SymbolResult symbolResult )
222+ static Token ? GetOptionTokenOrDefault ( SymbolResult symbolResult )
229223 {
230224 if ( symbolResult is not OptionResult optionResult )
231225 {
@@ -251,7 +245,7 @@ public static void HandleDebugSwitch(this ParseResult parseResult)
251245 /// If you are inside a command handler or 'normal' System.CommandLine code then you don't need this - the parse error handling
252246 /// will have covered these cases.
253247 /// </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 )
255249 {
256250 if ( parseResult . GetResult ( optionToGet ) is OptionResult optionResult &&
257251 ! parseResult . Errors . Any ( e => e . SymbolResult == optionResult ) )
0 commit comments