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

Your first cli-fp program

Documentation home · How do I...? · Command shapes · Options

Start with one root command when your program has one default action. The complete program below is also the repository's QuickStartDemo, which is compiled by the Windows and Linux example smoke checks.

The program follows the normal v1.4.x ownership model: THelloCommand descends from TBaseCommand, Main is its instance and owns --name, and App is the ICLIApplication that parses the invocation and calls Main.Execute.

program QuickStartDemo;

{$mode objfpc}{$H+}{$J-}

uses
  CLI.Interfaces, CLI.Application, CLI.Command;

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

function THelloCommand.Execute: Integer;
var
  PersonName: string;
begin
  if not GetParameterValue('--name', PersonName) then
    PersonName := 'World';
  WriteLn('Hello, ', PersonName, '!');
  Result := 0;
end;

var
  App: ICLIApplication;
  Main: THelloCommand;
begin
  Main := THelloCommand.Create('', 'Print a greeting');
  Main.AddStringParameter('-n', '--name', 'Name to greet', False, 'World');
  App := CreateCLIApplication('hello', '1.0.0', Main);
  Halt(App.Execute);
end.

Compile from a clone of this repository:

fpc -Fu./src ./examples/QuickStartDemo/QuickStartDemo.lpr
./examples/QuickStartDemo/QuickStartDemo --name Ada
fpc "-Fu.\src" .\examples\QuickStartDemo\QuickStartDemo.lpr
.\examples\QuickStartDemo\QuickStartDemo.exe --name Ada

Both commands print Hello, Ada!. Run --help to see generated usage and the option description.

What the program does

Next, choose whether your application should remain a root command or grow into named or nested commands. For common variations, go to How do I...?.