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,53 @@ 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+ return func ? . Invoke ( parseResult ) ?? [ ] ;
77+ }
6878
6979 public static CliOption < T > AllowSingleArgPerToken < T > ( this CliOption < T > option )
7080 {
@@ -94,9 +104,9 @@ public static CliOption<T> WithHelpDescription<T>(this CliOption<T> option, CliC
94104 return option ;
95105 }
96106
97- private static IEnumerable < string > ForwardedArguments ( string alias , IEnumerable < string > arguments )
107+ private static IEnumerable < string > ForwardedArguments ( string alias , IEnumerable < string > ? arguments )
98108 {
99- foreach ( string arg in arguments )
109+ foreach ( string arg in arguments ?? [ ] )
100110 {
101111 yield return alias ;
102112 yield return arg ;
@@ -113,34 +123,48 @@ public class ForwardedOption<T> : CliOption<T>, IForwardedOption
113123 {
114124 private Func < ParseResult , IEnumerable < string > > ForwardingFunction ;
115125
116- public ForwardedOption ( string name , params string [ ] aliases ) : base ( name , aliases ) { }
126+ public ForwardedOption ( string name , params string [ ] aliases ) : base ( name , aliases )
127+ {
128+ ForwardingFunction = _ => [ ] ;
129+ }
117130
118- public ForwardedOption ( string name , Func < ArgumentResult , T > parseArgument , string description = null )
131+ public ForwardedOption ( string name , Func < ArgumentResult , T > parseArgument , string ? description = null )
119132 : base ( name )
120133 {
121134 CustomParser = parseArgument ;
122135 Description = description ;
136+ ForwardingFunction = _ => [ ] ;
123137 }
124138
125- public ForwardedOption < T > SetForwardingFunction ( Func < T , IEnumerable < string > > func )
139+ public ForwardedOption < T > SetForwardingFunction ( Func < T ? , IEnumerable < string > > func )
126140 {
127141 ForwardingFunction = GetForwardingFunction ( func ) ;
128142 return this ;
129143 }
130144
131- public ForwardedOption < T > SetForwardingFunction ( Func < T , string > format )
145+ public ForwardedOption < T > SetForwardingFunction ( Func < T ? , string > format )
132146 {
133- ForwardingFunction = GetForwardingFunction ( ( o ) => new string [ ] { format ( o ) } ) ;
147+ ForwardingFunction = GetForwardingFunction ( ( o ) => [ format ( o ) ] ) ;
134148 return this ;
135149 }
136150
137- public ForwardedOption < T > SetForwardingFunction ( Func < T , ParseResult , IEnumerable < string > > func )
151+ public ForwardedOption < T > SetForwardingFunction ( Func < T ? , ParseResult , IEnumerable < string > > func )
138152 {
139- ForwardingFunction = ( ParseResult parseResult ) => parseResult . GetResult ( this ) is not null ? func ( parseResult . GetValue < T > ( this ) , parseResult ) : Array . Empty < string > ( ) ;
153+ ForwardingFunction = ( ParseResult parseResult ) =>
154+ {
155+ if ( parseResult . GetResult ( this ) is OptionResult argresult && argresult . GetValue < T > ( this ) is T validValue )
156+ {
157+ return func ( validValue , parseResult ) ?? [ ] ;
158+ }
159+ else
160+ {
161+ return [ ] ;
162+ }
163+ } ;
140164 return this ;
141165 }
142166
143- public Func < ParseResult , IEnumerable < string > > GetForwardingFunction ( Func < T , IEnumerable < string > > func )
167+ public Func < ParseResult , IEnumerable < string > > GetForwardingFunction ( Func < T ? , IEnumerable < string > > func )
144168 {
145169 return ( ParseResult parseResult ) => parseResult . GetResult ( this ) is not null ? func ( parseResult . GetValue < T > ( this ) ) : Array . Empty < string > ( ) ;
146170 }
@@ -153,7 +177,7 @@ public Func<ParseResult, IEnumerable<string>> GetForwardingFunction()
153177
154178 public class DynamicForwardedOption < T > : ForwardedOption < T > , IDynamicOption
155179 {
156- public DynamicForwardedOption ( string name , Func < ArgumentResult , T > parseArgument , string description = null )
180+ public DynamicForwardedOption ( string name , Func < ArgumentResult , T > parseArgument , string ? description = null )
157181 : base ( name , parseArgument , description )
158182 {
159183 }
0 commit comments