1212var app = ConsoleApp . Create ( ) ;
1313
1414// parse immediately
15- var verbose = app . AddOptionalGlobalOptions < bool > ( ref args , "-v|--verbose" ) ;
16- var noColor = app . AddOptionalGlobalOptions < bool > ( ref args , "--no-color" , "Don't colorize output." ) ;
15+ var verbose = app . AddGlobalOptions < bool > ( ref args , "-v|--verbose" ) ;
16+ var noColor = app . AddGlobalOptions < bool > ( ref args , "--no-color" , "Don't colorize output." ) ;
17+ var dryRun = app . AddGlobalOptions < bool > ( ref args , "--dry-run" ) ;
1718var prefixOutput = app . AddRequiredGlobalOptions < string > ( ref args , "--prefix-output" , "Prefix output with level." ) ;
18- var dryRun = app . AddOptionalGlobalOptions < bool > ( ref args , "--dry-run" ) ;
1919
2020app . ConfigureServices ( x =>
2121{
2222 // to use command body
23- x . AddSingleton < GlobalOptions > ( new GlobalOptions ( verbose , noColor , prefixOutput , dryRun ) ) ;
23+ x . AddSingleton < GlobalOptions > ( new GlobalOptions ( verbose , noColor , dryRun , prefixOutput ) ) ;
2424
2525 // variable for setup other DI
2626 x . AddLogging ( l =>
3737
3838app . Run ( args ) ;
3939
40- record GlobalOptions ( bool Verbose , bool NoColor , string PrefixOutput , bool DryRun ) ;
40+ record GlobalOptions ( bool Verbose , bool NoColor , bool DryRun , string PrefixOutput ) ;
4141
4242
4343public class Commands
@@ -62,9 +62,17 @@ internal static partial class ConsoleApp
6262 {
6363 internal partial class ConsoleAppBuilder
6464 {
65+ public T AddGlobalOptions < T > ( ref string [ ] args , string name , string description = "" , T defaultValue = default ( T ) )
66+ where T : IParsable < T >
67+ {
68+ return default ( T ) ;
69+ }
70+
6571 public T AddRequiredGlobalOptions < T > ( ref string [ ] args , [ ConstantExpected ] string name , [ ConstantExpected ] string description = "" )
6672 where T : IParsable < T >
6773 {
74+ if ( typeof ( T ) == typeof ( bool ) ) throw new ArgumentException ( ) ;
75+
6876 var aliasCount = name . AsSpan ( ) . Count ( "|" ) + 1 ;
6977 if ( aliasCount == 1 )
7078 {
@@ -87,12 +95,6 @@ public T AddRequiredGlobalOptions<T>(ref string[] args, [ConstantExpected] strin
8795
8896
8997
90- return default ( T ) ;
91- }
92-
93- public T AddOptionalGlobalOptions < T > ( ref string [ ] args , string name , string description = "" , T defaultValue = default ( T ) )
94- where T : IParsable < T >
95- {
9698 return default ( T ) ;
9799 }
98100 }
0 commit comments