Skip to content

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.

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);

Constraints enforce types at parse time — your handler only runs when input is valid:

ConstraintExampleAccepts
: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.

[Description("Add a new contact")]
public Contact Add(
[Description("Full name")] string name,
[Description("Email address")] string email)
{
// ...
}

This populates --help automatically:

$ myapp add --help
Usage: add {name} {email}
Arguments:
name Full name
email Email address
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 typeDefault output
stringPlain text
IEnumerable<T>Table
Any objectStructured (key: value)
Any + --jsonJSON
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.

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 IExitResult sets the process exit code, and a case implementing IReplResult renders and classifies as that result.
  • Sync, Task<T> and ValueTask<T> handlers can all return a union; a case that is itself a Task<T> or ValueTask<T> is awaited, and a case that is another union is unwrapped.
  • A default union, or a case holding null, behaves like a handler that returned null.

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.

[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.

Three range types cover different precision needs:

TypeStart/endDuration constraint
ReplDateRangeDateOnlyWhole days only
ReplDateTimeRangeDateTimeAny duration
ReplDateTimeOffsetRangeDateTimeOffsetAny 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-15
myapp report period --period 2024-01-15@30d
myapp logs range --range 2024-03-15T08:00..2024-03-15T18:00
myapp logs range --range 2024-03-15T08:00@8h

For the full list of accepted timestamp and duration formats, see Built-in Types & Formats.