55using System . CommandLine . Parsing ;
66using System . CommandLine . StaticCompletions ;
77
8+ #nullable enable
9+
810namespace Microsoft . DotNet . Cli
911{
1012 public static class OptionForwardingExtensions
1113 {
12- public static ForwardedOption < T > Forward < T > ( this ForwardedOption < T > option ) => option . SetForwardingFunction ( ( T o ) => new string [ ] { option . Name } ) ;
14+ public static ForwardedOption < T > Forward < T > ( this ForwardedOption < T > option ) => option . SetForwardingFunction ( ( T ? o ) => [ option . Name ] ) ;
15+
16+ public static ForwardedOption < T > ForwardAs < T > ( this ForwardedOption < T > option , string value ) => option . SetForwardingFunction ( ( T ? o ) => [ value ] ) ;
1317
14- public static ForwardedOption < T > ForwardAs < T > ( this ForwardedOption < T > option , string value ) => option . SetForwardingFunction ( ( T o ) => new string [ ] { value } ) ;
18+ public static ForwardedOption < T > ForwardAsSingle < T > ( this ForwardedOption < T > option , Func < T ? , string > format ) => option . SetForwardingFunction ( format ) ;
1519
16- public static ForwardedOption < T > ForwardAsSingle < T > ( this ForwardedOption < T > option , Func < T , string > format ) => option . SetForwardingFunction ( format ) ;
20+ public static ForwardedOption < bool > ForwardIfEnabled ( this ForwardedOption < bool > option , string value ) => option . SetForwardingFunction ( ( bool o ) => o == true ? [ value ] : [ ] ) ;
1721
1822 /// <summary>
1923 /// Set up an option to be forwaded as an output path to MSBuild
@@ -24,47 +28,60 @@ public static class OptionForwardingExtensions
2428 /// <returns>The option</returns>
2529 public static ForwardedOption < string > ForwardAsOutputPath ( this ForwardedOption < string > option , string outputPropertyName , bool surroundWithDoubleQuotes = false )
2630 {
27- return option . SetForwardingFunction ( ( string o ) =>
31+ return option . SetForwardingFunction ( ( string ? o ) =>
2832 {
33+ if ( o is null )
34+ {
35+ return [ ] ;
36+ }
2937 string argVal = CommandDirectoryContext . GetFullPath ( o ) ;
3038 if ( surroundWithDoubleQuotes )
3139 {
3240 // Not sure if this is necessary, but this is what "dotnet test" previously did and so we are
3341 // preserving the behavior here after refactoring
3442 argVal = TestCommandParser . SurroundWithDoubleQuotes ( argVal ) ;
3543 }
36- return new string [ ]
37- {
44+ return [
3845 $ "-property:{ outputPropertyName } ={ argVal } ",
3946 "-property:_CommandLineDefinedOutputPath=true"
40- } ;
47+ ] ;
4148 } ) ;
4249 }
4350
4451 public static ForwardedOption < string [ ] > ForwardAsProperty ( this ForwardedOption < string [ ] > option ) => option
4552 . SetForwardingFunction ( ( optionVals ) =>
46- optionVals
53+ ( optionVals ?? [ ] )
4754 . SelectMany ( Utils . MSBuildPropertyParser . ParseProperties )
4855 . Select ( keyValue => $ "{ option . Name } :{ keyValue . key } ={ keyValue . value } ")
4956 ) ;
5057
51- public static CliOption < T > ForwardAsMany < T > ( this ForwardedOption < T > option , Func < T , IEnumerable < string > > format ) => option . SetForwardingFunction ( format ) ;
58+ public static CliOption < T > ForwardAsMany < T > ( this ForwardedOption < T > option , Func < T ? , IEnumerable < string > > format ) => option . SetForwardingFunction ( format ) ;
5259
5360 public static CliOption < IEnumerable < string > > ForwardAsManyArgumentsEachPrefixedByOption ( this ForwardedOption < IEnumerable < string > > option , string alias ) => option . ForwardAsMany ( o => ForwardedArguments ( alias , o ) ) ;
5461
5562 public static IEnumerable < string > OptionValuesToBeForwarded ( this ParseResult parseResult , CliCommand command ) =>
5663 command . Options
5764 . OfType < IForwardedOption > ( )
58- . SelectMany ( o => o . GetForwardingFunction ( ) ( parseResult ) ) ?? Array . Empty < string > ( ) ;
65+ . Select ( o => o . GetForwardingFunction ( ) )
66+ . SelectMany ( f => f is not null ? f ( parseResult ) : Array . Empty < string > ( ) ) ;
5967
6068
61- public static IEnumerable < string > ForwardedOptionValues < T > ( this ParseResult parseResult , CliCommand command , string alias ) =>
62- command . Options ?
69+ public static IEnumerable < string > ForwardedOptionValues < T > ( this ParseResult parseResult , CliCommand command , string alias )
70+ {
71+ var func = command . Options ?
6372 . Where ( o => o . Name . Equals ( alias ) || o . Aliases . Contains ( alias ) ) ?
6473 . OfType < IForwardedOption > ( ) ?
6574 . FirstOrDefault ( ) ?
66- . GetForwardingFunction ( ) ( parseResult )
67- ?? Array . Empty < string > ( ) ;
75+ . GetForwardingFunction ( ) ;
76+ if ( func is not null )
77+ {
78+ return func ( parseResult ) ?? [ ] ;
79+ }
80+ else
81+ {
82+ return [ ] ;
83+ }
84+ }
6885
6986 public static CliOption < T > AllowSingleArgPerToken < T > ( this CliOption < T > option )
7087 {
@@ -94,9 +111,9 @@ public static CliOption<T> WithHelpDescription<T>(this CliOption<T> option, CliC
94111 return option ;
95112 }
96113
97- private static IEnumerable < string > ForwardedArguments ( string alias , IEnumerable < string > arguments )
114+ private static IEnumerable < string > ForwardedArguments ( string alias , IEnumerable < string > ? arguments )
98115 {
99- foreach ( string arg in arguments )
116+ foreach ( string arg in arguments ?? [ ] )
100117 {
101118 yield return alias ;
102119 yield return arg ;
@@ -113,34 +130,48 @@ public class ForwardedOption<T> : CliOption<T>, IForwardedOption
113130 {
114131 private Func < ParseResult , IEnumerable < string > > ForwardingFunction ;
115132
116- public ForwardedOption ( string name , params string [ ] aliases ) : base ( name , aliases ) { }
133+ public ForwardedOption ( string name , params string [ ] aliases ) : base ( name , aliases )
134+ {
135+ ForwardingFunction = _ => [ ] ;
136+ }
117137
118- public ForwardedOption ( string name , Func < ArgumentResult , T > parseArgument , string description = null )
138+ public ForwardedOption ( string name , Func < ArgumentResult , T > parseArgument , string ? description = null )
119139 : base ( name )
120140 {
121141 CustomParser = parseArgument ;
122142 Description = description ;
143+ ForwardingFunction = _ => [ ] ;
123144 }
124145
125- public ForwardedOption < T > SetForwardingFunction ( Func < T , IEnumerable < string > > func )
146+ public ForwardedOption < T > SetForwardingFunction ( Func < T ? , IEnumerable < string > > func )
126147 {
127148 ForwardingFunction = GetForwardingFunction ( func ) ;
128149 return this ;
129150 }
130151
131- public ForwardedOption < T > SetForwardingFunction ( Func < T , string > format )
152+ public ForwardedOption < T > SetForwardingFunction ( Func < T ? , string > format )
132153 {
133- ForwardingFunction = GetForwardingFunction ( ( o ) => new string [ ] { format ( o ) } ) ;
154+ ForwardingFunction = GetForwardingFunction ( ( o ) => [ format ( o ) ] ) ;
134155 return this ;
135156 }
136157
137- public ForwardedOption < T > SetForwardingFunction ( Func < T , ParseResult , IEnumerable < string > > func )
158+ public ForwardedOption < T > SetForwardingFunction ( Func < T ? , ParseResult , IEnumerable < string > > func )
138159 {
139- ForwardingFunction = ( ParseResult parseResult ) => parseResult . GetResult ( this ) is not null ? func ( parseResult . GetValue < T > ( this ) , parseResult ) : Array . Empty < string > ( ) ;
160+ ForwardingFunction = ( ParseResult parseResult ) =>
161+ {
162+ if ( parseResult . GetResult ( this ) is OptionResult argresult && argresult . GetValue < T > ( this ) is T validValue )
163+ {
164+ return func ( validValue , parseResult ) ?? [ ] ;
165+ }
166+ else
167+ {
168+ return [ ] ;
169+ }
170+ } ;
140171 return this ;
141172 }
142173
143- public Func < ParseResult , IEnumerable < string > > GetForwardingFunction ( Func < T , IEnumerable < string > > func )
174+ public Func < ParseResult , IEnumerable < string > > GetForwardingFunction ( Func < T ? , IEnumerable < string > > func )
144175 {
145176 return ( ParseResult parseResult ) => parseResult . GetResult ( this ) is not null ? func ( parseResult . GetValue < T > ( this ) ) : Array . Empty < string > ( ) ;
146177 }
@@ -153,7 +184,7 @@ public Func<ParseResult, IEnumerable<string>> GetForwardingFunction()
153184
154185 public class DynamicForwardedOption < T > : ForwardedOption < T > , IDynamicOption
155186 {
156- public DynamicForwardedOption ( string name , Func < ArgumentResult , T > parseArgument , string description = null )
187+ public DynamicForwardedOption ( string name , Func < ArgumentResult , T > parseArgument , string ? description = null )
157188 : base ( name , parseArgument , description )
158189 {
159190 }
0 commit comments