@@ -72,20 +72,27 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
7272 {
7373 using ( command . HasFilter ? sb . Nop : sb . BeginBlock ( "try" ) )
7474 {
75- // prepare commandArgs
75+ // prepare commandArgsMemory
7676 if ( command . HasFilter )
7777 {
7878 sb . AppendLine ( "var commandArgsMemory = context.InternalCommandArgs;" ) ; // already prepared and craeted ConsoleAppContext
7979 }
8080 else
8181 {
82- sb . AppendLine ( "var escapeIndex = args.AsSpan().IndexOf(\" --\" );" ) ;
83- if ( ! emitForBuilder )
82+ if ( hasConsoleAppContext || emitForBuilder )
8483 {
85- sb . AppendLine ( "var commandDepth = 0;" ) ;
86- }
84+ sb . AppendLine ( "var escapeIndex = args.AsSpan().IndexOf(\" --\" );" ) ;
85+ if ( ! emitForBuilder )
86+ {
87+ sb . AppendLine ( "var commandDepth = 0;" ) ;
88+ }
8789
88- sb . AppendLine ( "ReadOnlyMemory<string> commandArgsMemory = (escapeIndex == -1) ? args.AsMemory(commandDepth) : args.AsMemory(commandDepth, escapeIndex - commandDepth);" ) ;
90+ sb . AppendLine ( "ReadOnlyMemory<string> commandArgsMemory = (escapeIndex == -1) ? args.AsMemory(commandDepth) : args.AsMemory(commandDepth, escapeIndex - commandDepth);" ) ;
91+ }
92+ else
93+ {
94+ sb . AppendLine ( "ReadOnlyMemory<string> commandArgsMemory = args;" ) ;
95+ }
8996 }
9097
9198 sb . AppendLine ( $ "if (TryShowHelpOrVersion(commandArgsMemory.Span, { requiredParsableParameterCount } , { commandWithId . Id } )) return;") ;
@@ -104,18 +111,21 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
104111 if ( emitForBuilder )
105112 {
106113 sb . AppendLine ( "ConsoleAppContext context;" ) ;
107- using ( sb . BeginBlock ( "if (configureGlobalOptions == null )" ) )
114+ using ( hasConsoleAppContext ? sb . Nop : sb . BeginBlock ( "if (isRequireCallBuildAndSetServiceProvider )" ) )
108115 {
109- sb . AppendLine ( $ "context = new ConsoleAppContext(\" { command . Name } \" , args, commandArgsMemory, null, null, commandDepth, escapeIndex);") ;
110- }
111- using ( sb . BeginBlock ( "else" ) )
112- {
113- sb . AppendLine ( "var builder = new GlobalOptionsBuilder(commandArgsMemory);" ) ;
114- sb . AppendLine ( "var globalOptions = configureGlobalOptions(ref builder);" ) ;
115- sb . AppendLine ( $ "context = new ConsoleAppContext(\" { command . Name } \" , args, builder.RemainingArgs, null, globalOptions, commandDepth, escapeIndex);") ;
116- sb . AppendLine ( "commandArgsMemory = builder.RemainingArgs;" ) ;
116+ using ( sb . BeginBlock ( "if (configureGlobalOptions == null)" ) )
117+ {
118+ sb . AppendLine ( $ "context = new ConsoleAppContext(\" { command . Name } \" , args, commandArgsMemory, null, null, commandDepth, escapeIndex);") ;
119+ }
120+ using ( sb . BeginBlock ( "else" ) )
121+ {
122+ sb . AppendLine ( "var builder = new GlobalOptionsBuilder(commandArgsMemory);" ) ;
123+ sb . AppendLine ( "var globalOptions = configureGlobalOptions(ref builder);" ) ;
124+ sb . AppendLine ( $ "context = new ConsoleAppContext(\" { command . Name } \" , args, builder.RemainingArgs, null, globalOptions, commandDepth, escapeIndex);") ;
125+ sb . AppendLine ( "commandArgsMemory = builder.RemainingArgs;" ) ;
126+ }
127+ sb . AppendLine ( "BuildAndSetServiceProvider(context);" ) ;
117128 }
118- sb . AppendLine ( "BuildAndSetServiceProvider(context);" ) ;
119129
120130 if ( dllReference != null && dllReference . Value . HasHost )
121131 {
@@ -790,6 +800,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
790800 using ( sb . BeginBlock ( "public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IServiceCollection> configure)" ) )
791801 {
792802 sb . AppendLine ( "this.configureServices = (_, _, services) => configure(services);" ) ;
803+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
793804 sb . AppendLine ( "return this;" ) ;
794805 }
795806
@@ -799,6 +810,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
799810 // for backward-compatiblity, we chooce (IConfiguration, IServiceCollection) for two arguments overload
800811 sb . AppendLine ( "this.requireConfiguration = true;" ) ;
801812 sb . AppendLine ( "this.configureServices = (_, configuration, services) => configure(configuration, services);" ) ;
813+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
802814 sb . AppendLine ( "return this;" ) ;
803815 }
804816
@@ -807,6 +819,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
807819 {
808820 sb . AppendLine ( "this.requireConfiguration = true;" ) ;
809821 sb . AppendLine ( "this.configureServices = configure;" ) ;
822+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
810823 sb . AppendLine ( "return this;" ) ;
811824 }
812825 }
@@ -816,13 +829,15 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
816829 using ( sb . BeginBlock ( "public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IServiceCollection> configure)" ) )
817830 {
818831 sb . AppendLine ( "this.configureServices = (_, services) => configure(services);" ) ;
832+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
819833 sb . AppendLine ( "return this;" ) ;
820834 }
821835
822836 sb . AppendLine ( ) ;
823837 using ( sb . BeginBlock ( "public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<ConsoleAppContext, IServiceCollection> configure)" ) )
824838 {
825839 sb . AppendLine ( "this.configureServices = configure;" ) ;
840+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
826841 sb . AppendLine ( "return this;" ) ;
827842 }
828843 }
@@ -850,6 +865,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
850865 using ( sb . BeginBlock ( "public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ILoggingBuilder> configure)" ) )
851866 {
852867 sb . AppendLine ( "this.configureLogging = (_, _, logging) => configure(logging);" ) ;
868+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
853869 sb . AppendLine ( "return this;" ) ;
854870 }
855871
@@ -858,6 +874,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
858874 {
859875 sb . AppendLine ( "this.requireConfiguration = true;" ) ;
860876 sb . AppendLine ( "this.configureLogging = (_, configuration, logging) => configure(configuration, logging);" ) ;
877+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
861878 sb . AppendLine ( "return this;" ) ;
862879 }
863880
@@ -866,6 +883,7 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
866883 {
867884 sb . AppendLine ( "this.requireConfiguration = true;" ) ;
868885 sb . AppendLine ( "this.configureLogging = configure;" ) ;
886+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
869887 sb . AppendLine ( "return this;" ) ;
870888 }
871889 }
@@ -875,13 +893,15 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
875893 using ( sb . BeginBlock ( "public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ILoggingBuilder> configure)" ) )
876894 {
877895 sb . AppendLine ( "this.configureLogging = (_, logging) => configure(logging);" ) ;
896+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
878897 sb . AppendLine ( "return this;" ) ;
879898 }
880899
881900 sb . AppendLine ( ) ;
882901 using ( sb . BeginBlock ( "public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ConsoleAppContext, ILoggingBuilder> configure)" ) )
883902 {
884903 sb . AppendLine ( "this.configureLogging = configure;" ) ;
904+ sb . AppendLine ( "this.isRequireCallBuildAndSetServiceProvider = true;" ) ;
885905 sb . AppendLine ( "return this;" ) ;
886906 }
887907 }
0 commit comments