Skip to content

Commit 8960d59

Browse files
committed
check optionCandidate only [Argument] exists
1 parent 6e344c9 commit 8960d59

File tree

3 files changed

+5
-128
lines changed

3 files changed

+5
-128
lines changed
Lines changed: 3 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,7 @@
11
using ConsoleAppFramework;
2-
using Microsoft.Extensions.DependencyInjection;
3-
using Microsoft.Extensions.Hosting;
4-
using Microsoft.Extensions.Logging;
5-
using OpenTelemetry;
6-
using OpenTelemetry.Logs;
7-
using OpenTelemetry.Metrics;
8-
using OpenTelemetry.Resources;
9-
using OpenTelemetry.Trace;
10-
using System.Diagnostics;
112

12-
// Use SigNoz profiling: (view: http://localhost:8080/ )
13-
// git clone https://github.com/SigNoz/signoz.git
14-
// cd signoz/deploy/docker
15-
// docker compose up
16-
Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4317"); // 4317 or 4318
17-
18-
// crate builder from Microsoft.Extensions.Hosting.Host
19-
var builder = Host.CreateApplicationBuilder(args);
20-
21-
builder.Logging.AddOpenTelemetry(logging =>
3+
args = ["--x", "10", "y", "20"]; // missing argument
4+
ConsoleApp.Run(args, (int x, int y) =>
225
{
23-
logging.IncludeFormattedMessage = true;
24-
logging.IncludeScopes = true;
6+
Console.WriteLine(new { x, y });
257
});
26-
27-
builder.Services.AddOpenTelemetry()
28-
.UseOtlpExporter()
29-
.ConfigureResource(resource =>
30-
{
31-
resource.AddService("ConsoleAppFramework Telemetry Sample");
32-
})
33-
.WithMetrics(metrics =>
34-
{
35-
// configure for metrics
36-
metrics.AddRuntimeInstrumentation()
37-
.AddHttpClientInstrumentation();
38-
})
39-
.WithTracing(tracing =>
40-
{
41-
// configure for tracing
42-
tracing.SetSampler(new AlwaysOnSampler())
43-
.AddHttpClientInstrumentation()
44-
.AddSource(ConsoleAppFrameworkSampleActivitySource.Name);
45-
})
46-
.WithLogging(logging =>
47-
{
48-
// configure for logging
49-
});
50-
51-
var app = builder.ToConsoleAppBuilder();
52-
53-
app.Add<SampleCommand>();
54-
55-
// setup filter
56-
app.UseFilter<CommandTracingFilter>();
57-
58-
await app.RunAsync(args); // Run
59-
60-
public class SampleCommand(ILogger<SampleCommand> logger)
61-
{
62-
[Command("")]
63-
public async Task Run(CancellationToken cancellationToken)
64-
{
65-
using var httpClient = new HttpClient();
66-
67-
var ms = await httpClient.GetStringAsync("https://www.microsoft.com", cancellationToken);
68-
var google = await httpClient.GetStringAsync("https://www.google.com", cancellationToken);
69-
70-
logger.LogInformation("Sequential Query done.");
71-
72-
var ms2 = httpClient.GetStringAsync("https://www.microsoft.com", cancellationToken);
73-
var google2 = httpClient.GetStringAsync("https://www.google.com", cancellationToken);
74-
var apple2 = httpClient.GetStringAsync("https://www.apple.com", cancellationToken);
75-
await Task.WhenAll(ms2, google2, apple2);
76-
77-
logger.LogInformation("Parallel Query done.");
78-
}
79-
}
80-
81-
82-
83-
public static class ConsoleAppFrameworkSampleActivitySource
84-
{
85-
public const string Name = "ConsoleAppFrameworkSample";
86-
87-
public static ActivitySource Instance { get; } = new ActivitySource(Name);
88-
}
89-
90-
// Sample Activity filter
91-
internal class CommandTracingFilter(ConsoleAppFilter next) : ConsoleAppFilter(next)
92-
{
93-
public override async Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
94-
{
95-
using var activity = ConsoleAppFrameworkSampleActivitySource.Instance.StartActivity("CommandStart");
96-
97-
if (activity == null) // Telemtry is not listened
98-
{
99-
await Next.InvokeAsync(context, cancellationToken);
100-
}
101-
else
102-
{
103-
activity.SetTag("console_app.command_name", context.CommandName);
104-
activity.SetTag("console_app.command_args", string.Join(" ", context.EscapedArguments));
105-
106-
try
107-
{
108-
await Next.InvokeAsync(context, cancellationToken);
109-
activity.SetStatus(ActivityStatusCode.Ok);
110-
}
111-
catch (Exception ex)
112-
{
113-
if (ex is OperationCanceledException)
114-
{
115-
activity.SetStatus(ActivityStatusCode.Error, "Canceled");
116-
}
117-
else
118-
{
119-
activity.AddException(ex);
120-
activity.SetStatus(ActivityStatusCode.Error);
121-
}
122-
throw;
123-
}
124-
}
125-
}
126-
}

src/ConsoleAppFramework/Emitter.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,11 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
148148
{
149149
sb.AppendLine("var optionCandidate = name.Length > 1 && name[0] == '-' && !char.IsDigit(name[1]);");
150150
}
151-
else
152-
{
153-
sb.AppendLine("var optionCandidate = name.Length > 1 && name[0] == '-';");
154-
}
155151
sb.AppendLine();
156152

157153
if (!command.Parameters.All(p => !p.IsParsable || p.IsArgument))
158154
{
159-
using (sb.BeginBlock("if (optionCandidate)"))
155+
using (hasArgument ? sb.BeginBlock("if (optionCandidate)") : sb.Nop)
160156
{
161157
using (sb.BeginBlock("switch (name)"))
162158
{

tests/ConsoleAppFramework.GeneratorTests/RunTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void SyncRunShouldFailed()
8989
[Fact]
9090
public void MissingArgument()
9191
{
92-
verifier.Error("ConsoleApp.Run(args, (int x, int y) => { Console.Write((x + y)); });", "--x 10 y 20").ShouldContain("Required argument 'y' was not specified.");
92+
verifier.Error("ConsoleApp.Run(args, (int x, int y) => { Console.Write((x + y)); });", "--x 10 y 20").ShouldContain("Argument 'y' is not recognized.");
9393

9494
Environment.ExitCode.ShouldBe(1);
9595
Environment.ExitCode = 0;

0 commit comments

Comments
 (0)