@@ -65,120 +65,120 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
6565 // method signature
6666 using ( sb . BeginBlock ( $ "{ accessibility } { unsafeCode } { returnType } { methodName } (string[] args{ commandDepthEscapeIndex } { commandMethodType } { filterCancellationToken } )") )
6767 {
68- if ( emitForBuilder )
69- {
70- sb . AppendLine ( "var commandArgs = (escapeIndex == -1) ? args.AsSpan(commandDepth) : args.AsSpan(commandDepth, escapeIndex - commandDepth);" ) ;
71- }
72- else
68+ using ( command . HasFilter ? sb . Nop : sb . BeginBlock ( "try" ) )
7369 {
74- if ( hasConsoleAppContext )
70+ if ( emitForBuilder )
7571 {
76- sb . AppendLine ( "var escapeIndex = args.AsSpan().IndexOf(\" --\" );" ) ;
77- sb . AppendLine ( "var commandArgs = (escapeIndex == -1) ? args.AsSpan() : args.AsSpan(0, escapeIndex);" ) ;
72+ sb . AppendLine ( "var commandArgs = (escapeIndex == -1) ? args.AsSpan(commandDepth) : args.AsSpan(commandDepth, escapeIndex - commandDepth);" ) ;
7873 }
7974 else
8075 {
81- sb . AppendLine ( "var commandArgs = args.AsSpan();" ) ;
76+ if ( hasConsoleAppContext )
77+ {
78+ sb . AppendLine ( "var escapeIndex = args.AsSpan().IndexOf(\" --\" );" ) ;
79+ sb . AppendLine ( "var commandArgs = (escapeIndex == -1) ? args.AsSpan() : args.AsSpan(0, escapeIndex);" ) ;
80+ }
81+ else
82+ {
83+ sb . AppendLine ( "var commandArgs = args.AsSpan();" ) ;
84+ }
8285 }
83- }
8486
85- sb . AppendLine ( $ "if (TryShowHelpOrVersion(commandArgs, { requiredParsableParameterCount } , { commandWithId . Id } )) return;") ;
86- sb . AppendLine ( ) ;
87-
88- // prepare argument variables
89- if ( hasCancellationToken )
90- {
91- sb . AppendLine ( $ "using var posixSignalHandler = PosixSignalHandler.Register(Timeout, { cancellationTokenName } );") ;
87+ sb . AppendLine ( $ "if (TryShowHelpOrVersion(commandArgs, { requiredParsableParameterCount } , { commandWithId . Id } )) return;") ;
9288 sb . AppendLine ( ) ;
93- cancellationTokenName = "posixSignalHandler.Token" ;
94- }
9589
96- if ( hasConsoleAppContext )
97- {
98- if ( emitForBuilder )
90+ // prepare argument variables
91+ if ( hasCancellationToken )
9992 {
100- sb . AppendLine ( "ConsoleAppContext context;" ) ;
101- using ( sb . BeginBlock ( "if (configureGlobalOptions == null)" ) )
102- {
103- sb . AppendLine ( $ "context = new ConsoleAppContext(\" { command . Name } \" , args, args, null, null, commandDepth, escapeIndex);") ;
104- }
105- using ( sb . BeginBlock ( "else" ) )
106- {
107- sb . AppendLine ( "var builder = new GlobalOptionsBuilder(commandArgs);" ) ;
108- sb . AppendLine ( "var globalOptions = configureGlobalOptions(ref builder);" ) ;
109- sb . AppendLine ( $ "context = new ConsoleAppContext(\" { command . Name } \" , args, args, null, globalOptions, commandDepth, escapeIndex);") ;
110- sb . AppendLine ( "commandArgs = builder.RemainingArgs;" ) ;
111- }
112- sb . AppendLine ( "BuildAndSetServiceProvider(context);" ) ;
93+ sb . AppendLine ( $ "using var posixSignalHandler = PosixSignalHandler.Register(Timeout, { cancellationTokenName } );") ;
94+ sb . AppendLine ( ) ;
95+ cancellationTokenName = "posixSignalHandler.Token" ;
96+ }
11397
114- if ( dllReference != null && dllReference . Value . HasHost )
98+ if ( hasConsoleAppContext )
99+ {
100+ if ( emitForBuilder )
115101 {
116- sb . AppendLine ( "host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost ;" ) ;
117- using ( sb . BeginBlock ( "if (startHost && host ! = null)" ) )
102+ sb . AppendLine ( "ConsoleAppContext context ;" ) ;
103+ using ( sb . BeginBlock ( "if (configureGlobalOptions = = null)" ) )
118104 {
119- if ( isRunAsync )
120- {
121- sb . AppendLine ( $ "await host.StartAsync({ cancellationTokenName } );") ;
122- }
123- else
105+ sb . AppendLine ( $ "context = new ConsoleAppContext(\" { command . Name } \" , args, args, null, null, commandDepth, escapeIndex);") ;
106+ }
107+ using ( sb . BeginBlock ( "else" ) )
108+ {
109+ sb . AppendLine ( "var builder = new GlobalOptionsBuilder(commandArgs);" ) ;
110+ sb . AppendLine ( "var globalOptions = configureGlobalOptions(ref builder);" ) ;
111+ sb . AppendLine ( $ "context = new ConsoleAppContext(\" { command . Name } \" , args, args, null, globalOptions, commandDepth, escapeIndex);") ;
112+ sb . AppendLine ( "commandArgs = builder.RemainingArgs;" ) ;
113+ }
114+ sb . AppendLine ( "BuildAndSetServiceProvider(context);" ) ;
115+
116+ if ( dllReference != null && dllReference . Value . HasHost )
117+ {
118+ sb . AppendLine ( "host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;" ) ;
119+ using ( sb . BeginBlock ( "if (startHost && host != null)" ) )
124120 {
125- sb . AppendLine ( $ "host.StartAsync({ cancellationTokenName } ).GetAwaiter().GetResult();") ;
121+ if ( isRunAsync )
122+ {
123+ sb . AppendLine ( $ "await host.StartAsync({ cancellationTokenName } );") ;
124+ }
125+ else
126+ {
127+ sb . AppendLine ( $ "host.StartAsync({ cancellationTokenName } ).GetAwaiter().GetResult();") ;
128+ }
126129 }
127130 }
128131 }
132+ else
133+ {
134+ sb . AppendLine ( $ "var context = new ConsoleAppContext(\" { command . Name } \" , args, args, null, null, 0, escapeIndex);") ;
135+ }
136+ sb . AppendLine ( ) ;
129137 }
130- else
131- {
132- sb . AppendLine ( $ "var context = new ConsoleAppContext(\" { command . Name } \" , args, args, null, null, 0, escapeIndex);") ;
133- }
134- sb . AppendLine ( ) ;
135- }
136138
137- for ( var i = 0 ; i < command . Parameters . Length ; i ++ )
138- {
139- var parameter = command . Parameters [ i ] ;
140- if ( parameter . IsParsable )
139+ for ( var i = 0 ; i < command . Parameters . Length ; i ++ )
141140 {
142- var defaultValue = parameter . IsParams ? $ "({ parameter . ToTypeDisplayString ( ) } )[]"
143- : parameter . HasDefaultValue ? parameter . DefaultValueToString ( )
144- : $ "default({ parameter . Type . ToFullyQualifiedFormatDisplayString ( ) } )";
145- sb . AppendLine ( $ "var arg{ i } = { defaultValue } ;") ;
146- if ( parameter . RequireCheckArgumentParsed )
141+ var parameter = command . Parameters [ i ] ;
142+ if ( parameter . IsParsable )
147143 {
148- sb . AppendLine ( $ "var arg{ i } Parsed = false;") ;
144+ var defaultValue = parameter . IsParams ? $ "({ parameter . ToTypeDisplayString ( ) } )[]"
145+ : parameter . HasDefaultValue ? parameter . DefaultValueToString ( )
146+ : $ "default({ parameter . Type . ToFullyQualifiedFormatDisplayString ( ) } )";
147+ sb . AppendLine ( $ "var arg{ i } = { defaultValue } ;") ;
148+ if ( parameter . RequireCheckArgumentParsed )
149+ {
150+ sb . AppendLine ( $ "var arg{ i } Parsed = false;") ;
151+ }
149152 }
150- }
151- else if ( parameter . IsCancellationToken )
152- {
153- if ( command . HasFilter )
153+ else if ( parameter . IsCancellationToken )
154154 {
155- sb . AppendLine ( $ "var arg{ i } = cancellationToken;") ;
155+ if ( command . HasFilter )
156+ {
157+ sb . AppendLine ( $ "var arg{ i } = cancellationToken;") ;
158+ }
159+ else
160+ {
161+ sb . AppendLine ( $ "var arg{ i } = posixSignalHandler.Token;") ;
162+ }
156163 }
157- else
164+ else if ( parameter . IsConsoleAppContext )
158165 {
159- sb . AppendLine ( $ "var arg{ i } = posixSignalHandler.Token;") ;
166+ sb . AppendLine ( $ "var arg{ i } = context;") ;
167+ }
168+ else if ( parameter . IsFromServices )
169+ {
170+ var type = parameter . Type . ToFullyQualifiedFormatDisplayString ( ) ;
171+ sb . AppendLine ( $ "var arg{ i } = ({ type } )ServiceProvider!.GetService(typeof({ type } ))!;") ;
172+ }
173+ else if ( parameter . IsFromKeyedServices )
174+ {
175+ var type = parameter . Type . ToFullyQualifiedFormatDisplayString ( ) ;
176+ var line = $ "var arg{ i } = ({ type } )((Microsoft.Extensions.DependencyInjection.IKeyedServiceProvider)ServiceProvider).GetKeyedService(typeof({ type } ), { parameter . GetFormattedKeyedServiceKey ( ) } )!;";
177+ sb . AppendLine ( line ) ;
160178 }
161179 }
162- else if ( parameter . IsConsoleAppContext )
163- {
164- sb . AppendLine ( $ "var arg{ i } = context;") ;
165- }
166- else if ( parameter . IsFromServices )
167- {
168- var type = parameter . Type . ToFullyQualifiedFormatDisplayString ( ) ;
169- sb . AppendLine ( $ "var arg{ i } = ({ type } )ServiceProvider!.GetService(typeof({ type } ))!;") ;
170- }
171- else if ( parameter . IsFromKeyedServices )
172- {
173- var type = parameter . Type . ToFullyQualifiedFormatDisplayString ( ) ;
174- var line = $ "var arg{ i } = ({ type } )((Microsoft.Extensions.DependencyInjection.IKeyedServiceProvider)ServiceProvider).GetKeyedService(typeof({ type } ), { parameter . GetFormattedKeyedServiceKey ( ) } )!;";
175- sb . AppendLine ( line ) ;
176- }
177- }
178- sb . AppendLineIfExists ( command . Parameters . AsSpan ( ) ) ;
180+ sb . AppendLineIfExists ( command . Parameters . AsSpan ( ) ) ;
179181
180- using ( command . HasFilter ? sb . Nop : sb . BeginBlock ( "try" ) )
181- {
182182 if ( hasArgument )
183183 {
184184 sb . AppendLine ( "var argumentPosition = 0;" ) ;
@@ -904,7 +904,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
904904 using ( sb . BeginBlock ( "partial void StartHostAsyncIfNeeded(CancellationToken cancellationToken, ref Task task)" ) )
905905 {
906906 sb . AppendLine ( "Microsoft.Extensions.Hosting.IHost? host = ConsoleApp.ServiceProvider?.GetService(typeof(Microsoft.Extensions.Hosting.IHost)) as Microsoft.Extensions.Hosting.IHost;" ) ;
907- using ( sb . BeginBlock ( "if (startHost && host != null)" ) )
907+ using ( sb . BeginBlock ( "if (startHost && host != null)" ) )
908908 {
909909 sb . AppendLine ( "task = host.StartAsync(cancellationToken);" ) ;
910910 }
0 commit comments