Core Basics
This recipe covers the fundamentals using Repl.Core only — no DI, no hosting extras. It’s the minimal building block everything else is composed from.
The command graph
Section titled “The command graph”using System.ComponentModel;using Repl;using Repl.Parameters;
var store = new ContactStore();var commands = new ContactCommands(store);
var app = CoreReplApp.Create() .WithDescription("Core basics: minimal contacts app without DI.") .WithBanner("Try: list, add Alice alice@test.com, show 1, count");
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("debug reset", commands.Reset); // hidden below
return app.Run(args);Route constraints
Section titled “Route constraints”Constraints enforce types at parse time — your handler only runs when input is valid:
| Constraint | Example | Accepts |
|---|---|---|
:int | {id:int} | Integer |
:email | {email:email} | Valid email |
:guid | {token:guid} | GUID |
:bool | {flag:bool} | true/false |
:double | {rate:double} | Floating point |
Types are also inferred from parameter types — (int id) implies :int automatically.
Attributes for help and discovery
Section titled “Attributes for help and discovery”[Description("Add a new contact")]public Contact Add( [Description("Full name")] string name, [Description("Email address")] string email){ // ...}This populates --help automatically:
$ myapp add --helpUsage: add {name} {email}
Arguments: name Full name email Email addressHiding commands
Section titled “Hiding commands”app.Map("debug reset", commands.Reset);[Browsable(false)]public void Reset() { /* ... */ }[Browsable(false)] hides the command from --help and discovery, but it still runs if invoked directly. To hide a single option instead, see hidden options.
Return value rendering
Section titled “Return value rendering”| Return type | Default output |
|---|---|
string | Plain text |
IEnumerable<T> | Table |
| Any object | Structured (key: value) |
Any + --json | JSON |
Results.Error(...) | Error message, exit code 1 |
Results.Cancelled(...) | Message, exit code 1 |
Results.CompletedWithFailures(report) | The report, without an error prefix, exit code 1 |
Results.Exit(n) | Optional payload, explicit exit code n |
union (C# 15) | The active case, as if the handler had returned it directly (net11.0) |
An int returned by a handler is data, rendered like any other value; it never becomes the exit code. See Exit Codes & Signals.
Union return values (C# 15)
Section titled “Union return values (C# 15)”On net11.0, a handler can return a C# 15 union. Repl processes the active case in place of the returned union: classification, exit codes and rendering treat the case exactly as if the handler had returned it directly.
public record Contact(string Name);public record MissingContact(string Id);public union ContactLookup(Contact, MissingContact);
app.Map("contact {id}", (string id) => Find(id) is { } contact ? (ContactLookup)contact : new MissingContact(id));- A case implementing
IExitResultsets the process exit code, and a case implementingIReplResultrenders and classifies as that result. - Sync,
Task<T>andValueTask<T>handlers can all return a union; a case that is itself aTask<T>orValueTask<T>is awaited, and a case that is another union is unwrapped. - A
defaultunion, or a case holdingnull, behaves like a handler that returnednull.
Limits: a union passed as the payload of a Results helper (Results.Exit(3, lookup)) is rendered as an ordinary object, so return the union itself; a route value or option given for a union parameter is refused as a usage error, like any type Repl cannot convert (a union parameter can still come from the services); and unions are processed on the net11.0 build of Repl only, for types that implement the runtime’s System.Runtime.CompilerServices.IUnion. The full list is in Union return values.
Reusable options groups
Section titled “Reusable options groups”[ReplOptionsGroup]public class FilterOptions{ [ReplOption(Aliases = ["--limit"])] public int Limit { get; init; } = 20;
[ReplOption(Aliases = ["--sort"])] public string Sort { get; init; } = "name";}app.Map("list", (FilterOptions opts) => commands.List(opts));app.Map("report", (FilterOptions opts) => commands.Report(opts));Every command that accepts FilterOptions shares the same --limit / --sort flags. Avoid names already reserved by Repl — --format, --json, --no-logo, --help, --interactive, --output, and --answer:* are all taken.
Temporal ranges
Section titled “Temporal ranges”Three range types cover different precision needs:
| Type | Start/end | Duration constraint |
|---|---|---|
ReplDateRange | DateOnly | Whole days only |
ReplDateTimeRange | DateTime | Any duration |
ReplDateTimeOffsetRange | DateTimeOffset | Any duration |
app.Map("report period", (ReplDateRange period) => commands.Report(period));app.Map("logs range", (ReplDateTimeRange range) => commands.Logs(range));All three accept start..end and start@duration syntax:
myapp report period --period 2024-01-15..2024-02-15myapp report period --period 2024-01-15@30d
myapp logs range --range 2024-03-15T08:00..2024-03-15T18:00myapp logs range --range 2024-03-15T08:00@8hFor the full list of accepted timestamp and duration formats, see Built-in Types & Formats.
Further reading
Section titled “Further reading”- Routes & Parameters — full route constraint syntax, named options,
[ReplOptionsGroup], and parameter binding rules. - Dependency Injection — service lifetimes, static lambdas, and DI injection patterns for handlers.
- Best Practices — recommended patterns for mapping, output, and testing.