Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
44 lines (35 loc) · 1.56 KB
/
Copy pathProgram.cs
File metadata and controls
44 lines (35 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// A host small enough to read in one sitting, published as a native executable so that the
// engine's AOT compatibility is a build result rather than a claim. Everything it exercises is
// on the hot path of a real application: registration, routing, parsing, help, --json, completion.
using Respect.CommandLine;
var registry = new CommandRegistry();
var echo = CommandBuilder.Create("aot.echo", "echo")
.Summary("Write a message back.")
.Option("upper", o => o.Short('u').Summary("Write it in upper case."))
.Argument("message", a => a.Required().Summary("What to write."))
.Build();
using var registration = registry.Register(new CommandContribution
{
Descriptor = echo,
SourceId = "aot",
});
var application = new CommandLineApplication(registry, new ApplicationOptions
{
Name = "aot",
Version = "0.1.0-preview.1",
Summary = "Proof that Respect.CommandLine runs as a native executable.",
});
application.Handlers.Add(echo.Id, new EchoHandler());
application.AddCompletion();
return await application.RunAsync(args).ConfigureAwait(false);
internal sealed class EchoHandler : ICommandHandler
{
public ValueTask<CommandResult> ExecuteAsync(CommandContext context, CancellationToken cancellationToken)
{
var message = context.Values.GetValueOrDefault<string>("message") ?? string.Empty;
var text = context.Values.HasFlag("upper") ? message.ToUpperInvariant() : message;
context.Output.WriteLine(text);
context.Output.AddData("message", text);
return ValueTask.FromResult(CommandResult.Success);
}
}