Browse documentation

Overview

Overview

Start here

Your first CLIRunnable examplesGuide index

How do I...?

Common recipesShell completionProject generator

Learn

Commands and subcommandsOptions and validationOutput, errors, and progress

Reference

Public APICurrent limitations

Inside cli-fp

Technical design

Project

Contributing and support

Options and validation

Start here · How do I...? · API reference · Limitations

Options belong to a command object, not to the application globally. Define a TBaseCommand descendant, create its instance during program setup, register options on that instance, then retrieve accepted values inside that class's Execute method.

Register an option

This complete command pattern defines an option-owning command. The setup fragment creates its Cmd instance; every registration below is placed after that constructor call and therefore acts on a real TOptionsCommand object.

uses
  CLI.Command;

type
  TOptionsCommand = class(TBaseCommand)
  public
    function Execute: Integer; override;
  end;

function TOptionsCommand.Execute: Integer;
begin
  Result := 0;
end;
var
  Cmd: TOptionsCommand;
begin
  Cmd := TOptionsCommand.Create('configure', 'Configure the application');
  // Register Cmd options here, then register Cmd with the application.
end;

In the following table, Cmd means that exact TOptionsCommand instance.

NeedRegistration on Cmd
TextCmd.AddStringParameter('-n', '--name', 'Name', False, 'World')
IntegerCmd.AddIntegerParameter('-c', '--count', 'Count', True)
FloatCmd.AddFloatParameter('-r', '--rate', 'Rate', False, '1')
Presence flagCmd.AddFlag('-d', '--verbose', 'Verbose output')
Boolean with chosen defaultCmd.AddBooleanParameter('-b', '--color', 'Use colour', False, 'true')
ChoiceUse the enum registration below.
PathCmd.AddPathParameter('-p', '--path', 'Target path', True)
URLCmd.AddUrlParameter('-u', '--url', 'Repository URL', True)
PasswordCmd.AddPasswordParameter('-k', '--api-key', 'API key', True)
Date/timeCmd.AddDateTimeParameter('-t', '--time', 'Start time')
Comma-separated itemsCmd.AddArrayParameter('-a', '--items', 'Items')

The enum registration is also a setup fragment on the same Cmd instance; insert it with the table's registrations before App.RegisterCommand(Cmd):

Cmd.AddEnumParameter('-l', '--level', 'Level', 'debug|info|warn', False, 'info');

True in the required position makes an option required. An optional option with a default returns that default when it was omitted. Complete the program setup with App.RegisterCommand(Cmd), where App is the ICLIApplication variable created with CreateCLIApplication as shown in How-To.

Both helpers register ptBoolean and accept explicit true or false (including --verbose=false) as well as bare presence (true). AddFlag sets Required=False and defaults to 'false'; its default can be overridden. AddBooleanParameter takes explicit requiredness and default arguments. A non-empty default can satisfy requiredness when the option is omitted.

Flag matching is case-insensitive. The last occurrence wins across both aliases: --name Alice -n Bob and -n Alice --NAME=Bob both retrieve Bob. Equals syntax is supported for long options; short options use separate values. -v and --version, including case variants, are reserved for the application at root, named and nested scopes. Use -d or long-only --verbose for verbosity.

Use a validated value

Values are currently exposed as strings, even after integer, float, Boolean, or enum validation. Put conversion in the owning command's Execute. This replacement method for the TOptionsCommand pattern above needs SysUtils:

function TOptionsCommand.Execute: Integer;
var
  RawCount: string;
  Count: Integer;
begin
  if GetParameterValue('--count', RawCount) and
     TryStrToInt(RawCount, Count) then
    WriteLn('Count: ', Count);
  Result := 0;
end;

Use TryStrToFloat for a float and SameText(RawValue, 'true') for an explicit Boolean. This string-based lookup is a current API boundary, not an indication that validation was skipped.

Validation at a glance

For syntax rules and surprising boundaries, read Limitations and gotchas. For every public signature, use the API reference.