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.0')
Presence flagCmd.AddFlag('-v', '--verbose', 'Verbose output')
Explicit BooleanCmd.AddBooleanParameter('-c', '--color', 'Use colour', False, 'true')
Choice`Cmd.AddEnumParameter('-l', '--level', 'Level', 'debuginfowarn', False, 'info')`
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')

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.

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.