-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (70 loc) · 2.34 KB
/
Program.cs
File metadata and controls
83 lines (70 loc) · 2.34 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.ComponentModel;
using Repl;
using Repl.Parameters;
// Sample goal:
// - minimal CoreReplApp (no DI package)
// - simple contact commands + metadata attributes
var store = new ContactStore();
var commands = new ContactCommands(store);
var app = CoreReplApp.Create()
.WithDescription("Core basics sample: minimal contacts REPL without DI dependencies.")
.WithBanner("""
Try: list, add Alice alice@test.com, show 1, count
Also: error (exception handling), debug reset
""");
app.Map("list", commands.List);
app.Map("add {name} {email:email}", commands.Add);
app.Map("show {id:int}", commands.Show);
app.Map("count", commands.Count);
app.Map("report period", commands.ReportPeriod);
app.Map("error", ErrorCommand);
app.Map("debug reset", commands.Reset);
return app.Run(args);
static object ErrorCommand() =>
throw new ApplicationException("this is an error.");
file sealed class ContactCommands(ContactStore store)
{
[Description("List all contacts.")]
public List<Contact> List(SampleOutputOptions output)
{
_ = output;
return [.. store.List()];
}
[Description("Add a new contact.")]
public Contact Add(
[Description("Contact full name")] string name,
[Description("Email address")] string email)
=> store.Add(name, email);
[Description("Show one contact by id.")]
public object Show(
[Description("Contact numeric id")] int id,
SampleOutputOptions output)
{
_ = output;
return store.Find(id) is { } contact
? contact
: Results.NotFound($"Contact '{id}' was not found.");
}
[Description("Return the number of contacts.")]
public object Count() => Results.Success("Contact count.", store.Count());
[Description("Render a date-only reporting period from a temporal range literal.")]
public string ReportPeriod(ReplDateRange period) =>
$"Reporting from {period.From:yyyy-MM-dd} to {period.To:yyyy-MM-dd} ({store.Count()} contacts in memory).";
[Description("Reset in-memory sample data.")]
[Browsable(false)]
public object Reset()
{
store.Reset();
return Results.Success("Data reset complete.");
}
}
[ReplOptionsGroup]
file sealed class SampleOutputOptions
{
[ReplOption(Aliases = ["-f"])]
[Description("Output format for this command.")]
public string Format { get; set; } = "text";
[ReplOption(ReverseAliases = ["--no-verbose"])]
[Description("Enable verbose output.")]
public bool Verbose { get; set; }
}