11using ConsoleAppFramework ;
22using GeneratorSandbox ;
3+ using Microsoft . Extensions . DependencyInjection ;
4+ using Microsoft . Extensions . Logging ;
5+ using System . Diagnostics . CodeAnalysis ;
6+ using System . Drawing ;
37
48// fail
59//await ConsoleApp.RunAsync(args, Commands.Save);
610
711
812var app = ConsoleApp . Create ( ) ;
913
14+ // 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." ) ;
17+ var prefixOutput = app . AddRequiredGlobalOptions < string > ( ref args , "--prefix-output" , "Prefix output with level." ) ;
18+ var dryRun = app . AddOptionalGlobalOptions < bool > ( ref args , "--dry-run" ) ;
1019
11- app . Run ( args , true , true , true ) ;
20+ app . ConfigureServices ( x =>
21+ {
22+ // to use command body
23+ x . AddSingleton < GlobalOptions > ( new GlobalOptions ( verbose , noColor , prefixOutput , dryRun ) ) ;
1224
25+ // variable for setup other DI
26+ x . AddLogging ( l =>
27+ {
28+ var console = l . AddSimpleConsole ( ) ;
29+ if ( verbose )
30+ {
31+ console . SetMinimumLevel ( LogLevel . Trace ) ;
32+ }
33+ } ) ;
34+ } ) ;
1335
14- // fail
15- // await ConsoleApp.RunAsync(args, async () => await Task.Delay(1000, CancellationToken.None));
36+ app . Add < Commands > ( "" ) ;
37+
38+ app . Run ( args ) ;
39+
40+ record GlobalOptions ( bool Verbose , bool NoColor , string PrefixOutput , bool DryRun ) ;
1641
1742
1843public class Commands
1944{
2045 /// <summary>
2146 /// Some sort of save command.
2247 /// </summary>
23- public async Task < int > Save ( CancellationToken ct )
48+ public async Task < int > Save ( int x , int y )
2449 {
2550
2651 await Task . Delay ( 1000 ) ;
@@ -30,3 +55,46 @@ public async Task<int> Save(CancellationToken ct)
3055
3156
3257// `using var posixSignalHandler = PosixSignalHandler.Register(Timeout);`
58+
59+ namespace ConsoleAppFramework
60+ {
61+ internal static partial class ConsoleApp
62+ {
63+ internal partial class ConsoleAppBuilder
64+ {
65+ public T AddRequiredGlobalOptions < T > ( ref string [ ] args , [ ConstantExpected ] string name , [ ConstantExpected ] string description = "" )
66+ where T : IParsable < T >
67+ {
68+ var aliasCount = name . AsSpan ( ) . Count ( "|" ) + 1 ;
69+ if ( aliasCount == 1 )
70+ {
71+ // TryParse...
72+ }
73+ else
74+ {
75+ string [ ] aliases = name . Split ( '|' , StringSplitOptions . RemoveEmptyEntries ) . Select ( x => x . Trim ( ) ) . ToArray ( ) ;
76+
77+
78+ }
79+
80+ //for (int i = 0; i < args.Length; i++)
81+ //{
82+ // if (args[i] == name)
83+ // {
84+ // args.AsSpan().Count("|");
85+ // }
86+ //}
87+
88+
89+
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+ {
96+ return default ( T ) ;
97+ }
98+ }
99+ }
100+ }
0 commit comments