Making Options Required

How to make command-line options required instead of optional in Spectre.Console.Cli

By design, options (flags like --name or -n) are optional—that's why they're called "options." However, there are cases where you want a named option that users must provide, such as specifying a target environment or API key.

What We're Building

A deployment command where --environment and --version are mandatory. Missing one produces a clear error:

Required options demonstration

Use the isRequired Parameter

The simplest approach is to use the isRequired parameter on the CommandOption attribute.

public class Settings : CommandSettings
{
    // Use `isRequired` parameter on `CommandOption`
    [CommandOption("-e|--environment <TARGET>", isRequired: true)]
    [Description("Target environment")]
    public required string Environment { get; init; }
  
    [CommandOption("-v|--version <VERSION>", isRequired: true)]
    [Description("Version to deploy")]
    public required string Version { get; init; }
  
    [CommandOption("--dry-run")]
    [Description("Preview changes without applying them")]
    public bool DryRun { get; init; }
}

Help output marks these options clearly:

OPTIONS:
    -e, --environment    Target environment. Required
    -v, --version        Version to deploy. Required
        --dry-run        Preview changes without applying them

Validate Across Multiple Options

For more complex scenarios—like requiring at least one of several options, or preventing two options from being used together—override the Validate() method in your settings class:

public override ValidationResult Validate()
{
    if (string.IsNullOrEmpty(ConnectionString) && string.IsNullOrEmpty(Host))
    {
        return ValidationResult.Error(
            "Provide either --connection-string or --host");
    }
    return ValidationResult.Success();
}

This gives you full control over error messages and allows for validation logic that can't be expressed with attributes alone.

See Also