Skip to content

Commit bf1744b

Browse files
committed
fix buildserviceprovider case
1 parent 03bc452 commit bf1744b

File tree

2 files changed

+24
-299
lines changed

2 files changed

+24
-299
lines changed
Lines changed: 23 additions & 298 deletions
Original file line numberDiff line numberDiff line change
@@ -1,316 +1,41 @@
11
using ConsoleAppFramework;
2-
using GeneratorSandbox;
32
using Microsoft.Extensions.DependencyInjection;
43
using Microsoft.Extensions.Logging;
5-
using System.ComponentModel.DataAnnotations;
6-
using System.Diagnostics.CodeAnalysis;
7-
using System.Runtime.CompilerServices;
84

9-
// fail
10-
//await ConsoleApp.RunAsync(args, Commands.Save);
5+
args = ["--x", "10", "--y", "20", "-v", "--prefix-output", "takoyakix"];
116

7+
var app = ConsoleApp.Create();
128

13-
14-
//var app = ConsoleApp.Create();
15-
16-
17-
//args = ["--x", "10", "--y", "20", "-v", "--prefix-output", "takoyakix"];
18-
19-
20-
//// Enum.TryParse<Fruit>("", true,
21-
//// parse immediately
22-
23-
24-
//app.ConfigureGlobalOption(x =>
25-
//{
26-
// var verbose = x.AddGlobalOption<bool>($"takoyaki", "", true);
27-
// var noColor = x.AddGlobalOption<bool>("--no-color", "Don't colorize output.");
28-
// var dryRun = x.AddGlobalOption<bool>("--dry-run");
29-
// var prefixOutput = x.AddRequiredGlobalOption<string>("--prefix-output|-pp|-po", "Prefix output with level.");
30-
31-
// return new GlobalOptions(verbose, noColor, dryRun, prefixOutput);
32-
//});
33-
34-
//app.ConfigureServices(x =>
35-
//{
36-
37-
// // new ConsoleAppContext("",
38-
39-
40-
41-
42-
43-
44-
// // to use command body
45-
// //x.AddSingleton<GlobalOptions>(new GlobalOptions(verbose, noColor, dryRun, prefixOutput));
46-
47-
// //// variable for setup other DI
48-
// //x.AddLogging(l =>
49-
// //{
50-
// // var console = l.AddSimpleConsole();
51-
// // if (verbose)
52-
// // {
53-
// // console.SetMinimumLevel(LogLevel.Trace);
54-
// // }
55-
// //});
56-
//});
57-
58-
//app.Add<Commands>("");
59-
60-
//app.Run(args);
61-
62-
//var app = ConsoleApp.Create();
63-
64-
65-
//app.ConfigureGlobalOptions((ref ConsoleApp.GlobalOptionsBuilder builder) =>
66-
//{
67-
// var verbose = builder.AddGlobalOption<bool>($"-v", "", true);
68-
// var noColor = builder.AddGlobalOption<bool>("--no-color", "Don't colorize output.");
69-
// var dryRun = builder.AddGlobalOption<bool>("--dry-run");
70-
// var prefixOutput = builder.AddRequiredGlobalOption<string>("--prefix-output|-pp|-po", "Prefix output with level.");
71-
72-
// return new GlobalOptions(verbose, noColor, dryRun, prefixOutput);
73-
//});
74-
75-
76-
//app.Add("", async (int x, int y, ConsoleAppContext context, CancellationToken cancellationToken) =>
77-
//{
78-
// Console.WriteLine("OK");
79-
// await Task.Delay(TimeSpan.FromSeconds(1));
80-
// Console.WriteLine(context.CommandName + ":" + (x, y));
81-
//});
82-
83-
//app.Add("tako", (int x, int y, ConsoleAppContext context) =>
84-
//{
85-
// Console.WriteLine(context.CommandName);
86-
//});
87-
88-
//app.UseFilter<NopFilter>();
89-
90-
//await app.RunAsync(args);
91-
92-
var builder = ConsoleApp.Create();
93-
94-
95-
builder.ConfigureDefaultConfiguration()
96-
.ConfigureServices((c, x) =>
97-
{
98-
99-
});
100-
101-
builder.UseFilter<NopFilter1>();
102-
builder.UseFilter<NopFilter2>();
103-
104-
builder.Add<MyClass>();
105-
106-
await builder.RunAsync(args);
107-
108-
[ConsoleAppFilter<NopFilter3>]
109-
[ConsoleAppFilter<NopFilter4>]
110-
public class MyClass
111-
{
112-
[ConsoleAppFilter<NopFilter5>]
113-
[ConsoleAppFilter<NopFilter6>]
114-
public void Hello()
115-
{
116-
Console.Write("abcde");
117-
}
118-
}
119-
120-
internal class NopFilter1(ConsoleAppFilter next)
121-
: ConsoleAppFilter(next)
122-
{
123-
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
124-
{
125-
Console.Write(1);
126-
return Next.InvokeAsync(context, cancellationToken);
127-
}
128-
}
129-
130-
internal class NopFilter2(ConsoleAppFilter next)
131-
: ConsoleAppFilter(next)
9+
app.ConfigureGlobalOptions((ref ConsoleApp.GlobalOptionsBuilder builder) =>
13210
{
133-
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
134-
{
135-
Console.Write(2);
136-
return Next.InvokeAsync(context, cancellationToken);
137-
}
138-
}
11+
var verbose = builder.AddGlobalOption<bool>($"-v", "", true);
12+
var noColor = builder.AddGlobalOption<bool>("--no-color", "Don't colorize output.");
13+
var dryRun = builder.AddGlobalOption<bool>("--dry-run");
14+
var prefixOutput = builder.AddRequiredGlobalOption<string>("--prefix-output|-pp|-po", "Prefix output with level.");
13915

140-
internal class NopFilter3(ConsoleAppFilter next)
141-
: ConsoleAppFilter(next)
142-
{
143-
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
144-
{
145-
Console.Write(3);
146-
return Next.InvokeAsync(context, cancellationToken);
147-
}
148-
}
16+
return new GlobalOptions(verbose, noColor, dryRun, prefixOutput);
17+
});
14918

150-
internal class NopFilter4(ConsoleAppFilter next)
151-
: ConsoleAppFilter(next)
19+
app.ConfigureServices((context, configuration, collection) =>
15220
{
153-
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
154-
{
155-
Console.Write(4);
156-
return Next.InvokeAsync(context, cancellationToken);
157-
}
158-
}
21+
var globalOptions = (GlobalOptions)context.GlobalOptions;
15922

160-
internal class NopFilter5(ConsoleAppFilter next)
161-
: ConsoleAppFilter(next)
162-
{
163-
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
164-
{
165-
Console.Write(5);
166-
return Next.InvokeAsync(context, cancellationToken);
167-
}
168-
}
23+
// simply use for filter/command body
24+
collection.AddSingleton(globalOptions);
16925

170-
internal class NopFilter6(ConsoleAppFilter next)
171-
: ConsoleAppFilter(next)
172-
{
173-
public override Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
26+
// variable for setup other DI
27+
collection.AddLogging(logging =>
17428
{
175-
Console.Write(6);
176-
return Next.InvokeAsync(context, cancellationToken);
177-
}
178-
}
179-
180-
181-
public record GlobalOptions(bool Verbose, bool NoColor, bool DryRun, string PrefixOutput);
182-
183-
184-
internal delegate object TakoyakiX(FooStruct builder);
185-
186-
187-
public enum Fruit
188-
{
189-
Orange, Apple, Grape
190-
}
191-
192-
193-
194-
195-
public ref struct FooStruct
196-
{
197-
}
198-
199-
200-
public class Commands(GlobalOptions globalOptions)
201-
{
202-
/// <summary>
203-
/// Some sort of save command.
204-
/// </summary>
205-
public async Task<int> Save(int x, int y, ConsoleAppContext ctx)
206-
{
207-
Console.WriteLine(globalOptions);
208-
Console.WriteLine("Called this:" + new { x, y });
209-
Console.WriteLine(ctx.CommandName);
210-
await Task.Delay(1000);
211-
return 0;
212-
}
213-
}
214-
215-
216-
namespace ConsoleAppFramework
217-
{
218-
internal static partial class ConsoleApp
219-
{
220-
internal partial class ConsoleAppBuilder
29+
var console = logging.AddSimpleConsole();
30+
if (globalOptions.Verbose)
22131
{
222-
223-
224-
225-
226-
227-
228-
229-
private void RunCommand0_2(string[] args, int commandDepth, int escapeIndex, Action<int, int, global::ConsoleAppFramework.ConsoleAppContext> command, CancellationToken __ExternalCancellationToken__)
230-
{
231-
var commandArgs = (escapeIndex == -1) ? args.AsSpan(commandDepth) : args.AsSpan(commandDepth, escapeIndex - commandDepth);
232-
if (TryShowHelpOrVersion(commandArgs, 2, 0)) return;
233-
234-
ConsoleAppContext context = default!;
235-
//if (configureGlobalOptions == null)
236-
//{
237-
// context = new ConsoleAppContext("", args, null, null, commandDepth, escapeIndex);
238-
//}
239-
//else
240-
//{
241-
// var builder = new GlobalOptionsBuilder(commandArgs);
242-
// var globalOptions = configureGlobalOptions(ref builder);
243-
// context = new ConsoleAppContext("", args, null, globalOptions, commandDepth, escapeIndex);
244-
// commandArgs = builder.RemainingArgs;
245-
//}
246-
//BuildAndSetServiceProvider(context);
247-
248-
var arg0 = default(int);
249-
var arg0Parsed = false;
250-
var arg1 = default(int);
251-
var arg1Parsed = false;
252-
var arg2 = context;
253-
254-
try
255-
{
256-
for (int i = 0; i < commandArgs.Length; i++)
257-
{
258-
var name = commandArgs[i];
259-
260-
switch (name)
261-
{
262-
case "--x":
263-
{
264-
if (!TryIncrementIndex(ref i, commandArgs.Length) || !int.TryParse(commandArgs[i], out arg0)) { ThrowArgumentParseFailed("x", commandArgs[i]); }
265-
arg0Parsed = true;
266-
continue;
267-
}
268-
case "--y":
269-
{
270-
if (!TryIncrementIndex(ref i, commandArgs.Length) || !int.TryParse(commandArgs[i], out arg1)) { ThrowArgumentParseFailed("y", commandArgs[i]); }
271-
arg1Parsed = true;
272-
continue;
273-
}
274-
default:
275-
if (string.Equals(name, "--x", StringComparison.OrdinalIgnoreCase))
276-
{
277-
if (!TryIncrementIndex(ref i, commandArgs.Length) || !int.TryParse(commandArgs[i], out arg0)) { ThrowArgumentParseFailed("x", commandArgs[i]); }
278-
arg0Parsed = true;
279-
continue;
280-
}
281-
if (string.Equals(name, "--y", StringComparison.OrdinalIgnoreCase))
282-
{
283-
if (!TryIncrementIndex(ref i, commandArgs.Length) || !int.TryParse(commandArgs[i], out arg1)) { ThrowArgumentParseFailed("y", commandArgs[i]); }
284-
arg1Parsed = true;
285-
continue;
286-
}
287-
ThrowArgumentNameNotFound(name);
288-
break;
289-
}
290-
}
291-
if (!arg0Parsed) ThrowRequiredArgumentNotParsed("x");
292-
if (!arg1Parsed) ThrowRequiredArgumentNotParsed("y");
293-
294-
command(arg0!, arg1!, arg2!);
295-
}
296-
catch (Exception ex)
297-
{
298-
Environment.ExitCode = 1;
299-
if (ex is ValidationException or ArgumentParseFailedException)
300-
{
301-
LogError(ex.Message);
302-
}
303-
else
304-
{
305-
LogError(ex.ToString());
306-
}
307-
}
308-
}
309-
310-
32+
console.SetMinimumLevel(LogLevel.Trace);
31133
}
34+
});
35+
});
31236

37+
app.Add("", (int x, int y, [FromServices]GlobalOptions globalOptions) => Console.WriteLine(x + y + ":" + globalOptions));
31338

39+
app.Run(args);
31440

315-
}
316-
}
41+
internal record GlobalOptions(bool Verbose, bool NoColor, bool DryRun, string PrefixOutput);

src/ConsoleAppFramework/Emitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
9595
cancellationTokenName = "posixSignalHandler.Token";
9696
}
9797

98-
if (hasConsoleAppContext)
98+
if (hasConsoleAppContext || emitForBuilder)
9999
{
100100
if (emitForBuilder)
101101
{

0 commit comments

Comments
 (0)