Class TCommandBuilder
Unit
Declaration
type TCommandBuilder = class(TInterfacedObject, ICommandBuilder)
Description
Class that implements the ICommandBuilder interface, its main purpose is to configure the arguments, commands and options accepted by the tool. Central point of the library, responsible for comparing and validating the parameters passed by the command line with the configured parameters, later executing the callback linked to the localized command.
Hierarchy
- TInterfacedObject
- TCommandBuilder
Overview
Methods
constructor Create(AExeName: String); |
|
class function New(ATitle: String): ICommandBuilder; |
|
function AddCommand(const ACommand: string; const ADescription: string = ''; ACallback: TCommandCallback = nil; AConstraints: TCommandConstraints = []): ICommandBuilder; overload; |
|
function AddCommand(const ACommand: TAddCommandCallback): ICommandBuilder; overload; |
|
function Description(const ADescription: string): ICommandBuilder; |
|
function CheckConstraints(AConstraints: TCommandConstraints): ICommandBuilder; |
|
function OnExecute(ACallback: TCommandCallback): ICommandBuilder; |
|
function AddArgument(const ADescription: string; AConstraint: TArgumentConstraint): ICommandBuilder; |
|
function AddOption(const AFlag, AName, ADescription: string; ANotAllowedFlags: TArray<string> = nil; AConstraint: TOptionConstraint = ocNoValue): ICommandBuilder; |
|
procedure Parse; |
|
function Validate: TArray<string>; |
|
procedure Execute; |
|
function Run: ICommandBuilder; |
|
function CommandSelected: ICommand; |
|
function SetCommandSelected(ACommand: ICommand): ICommandBuilder; |
|
function CommandAsArgument: ICommand; |
|
function SetCommandAsArgument(ACommand: ICommand): ICommandBuilder; |
|
function GetCommandsFound: Integer; |
|
function GetDefaultCommand: ICommand; |
|
function CheckOption(const AOption: string): Boolean; overload; |
|
function CheckOption(const AOption: string; out AValue: string): Boolean; overload; |
|
function GetRawArguments: TArray<string>; |
|
function GetRawOptions: TArray<string>; |
|
function ExeName: string; |
|
function HasCommands: Boolean; |
|
function HasArguments: Boolean; |
|
function HasOptions: Boolean; |
|
function UseArguments(AArguments: TArray<string>): ICommandBuilder; |
|
function UseColorTheme(ATheme: TColorTheme): ICommandBuilder; |
Properties
property Commands: TArray<ICommand> read GetCommands; |
|
property Arguments: TArray<IArgument> read GetArguments; |
|
property ParsedOptions: TArray<IOption> read GetParsedOptions; |
|
property ParsedArguments: TArray<IArgument> read GetParsedArguments; |
|
property InputLn: TInputLnCallback read GetInputLn write SetInputLn; |
|
property Output: TOutputCallback read GetOutput write SetOutput; |
|
property OutputColor: TOutputColorCallback read GetOutputColor write SetOutputColor; |
|
property ColorTheme: TColorTheme read GetColorTheme write SetColorTheme; |
|
property Title: string read GetTitle write SetTitle; |
|
property UseShortDescriptions: boolean read GetUseShortDescriptions write SetUseShortDescriptions; |
|
property State: string read GetState write SetState; |
Description
Methods
constructor Create(AExeName: String); |
|
Default class constructor. Creates an instance with a exe name that will be displayed to the user when using commands like UsageCommand or VersionCommand.
Parameters
|
class function New(ATitle: String): ICommandBuilder; |
|
A class factory for command builder. It allows the initialization of the builder with a brief description and will extract the application executable name that will be used to display usage info on how to use the tool.
Parameters
|
function AddCommand(const ACommand: string; const ADescription: string = ''; ACallback: TCommandCallback = nil; AConstraints: TCommandConstraints = []): ICommandBuilder; overload; |
|
Adds a command that will be available to the user of the command line application. This command will be added to the TCommandBuilder.Commands property.
Parameters
|
function AddCommand(const ACommand: TAddCommandCallback): ICommandBuilder; overload; |
|
Using a callback to add a command to the CommandBuilder. The main purpose of this method is to allow the use of the fluent interface. Ex: CommandBuilder.AddCommand(@Command.Usage.Registry);
Parameters
|
function Description(const ADescription: string): ICommandBuilder; |
|
Set the description for the last command that was added.
Description of the command that best describes its purpose.</summay |
function CheckConstraints(AConstraints: TCommandConstraints): ICommandBuilder; |
|
Set the constraints for the last command that was added.
Parameters
|
function OnExecute(ACallback: TCommandCallback): ICommandBuilder; |
|
Set the callback for the last command that was added.
Parameters
|
function AddArgument(const ADescription: string; AConstraint: TArgumentConstraint): ICommandBuilder; |
|
Adds an argument to allow the user to pass a text argument via the command line. This argument will be added to the TCommandBuilder.Arguments property. After parse the value can be obtained through the value property of an item in the arguments array.
Parameters
|
function AddOption(const AFlag, AName, ADescription: string; ANotAllowedFlags: TArray<string> = nil; AConstraint: TOptionConstraint = ocNoValue): ICommandBuilder; |
|
Adds an option to the last added command. For both the short and long options.
Parameters
|
procedure Parse; |
|
Parses parameters passed via command line, matching command names and arguments for further validation. |
procedure Execute; |
|
Invokes the callback procedure configured for the command found once parse and validation have been successful. It passes the TCommandBuilder itself as a parameter to the callback so that it is possible to call the CheckOption method to validate the presence of a certain option, or to obtain the value of an expected argument through the Arguments[n].Value property. |
function Run: ICommandBuilder; |
|
Executes the TCommandBuilder.Parse, |
function CommandSelected: ICommand; |
|
Returns the selected command after Parse. |
function SetCommandSelected(ACommand: ICommand): ICommandBuilder; |
|
Manually set the selected command. Mostly used for test purpose. |
function CommandAsArgument: ICommand; |
|
Returns a possible command that was passed as an argument to another command. Value is available after Parse. |
function SetCommandAsArgument(ACommand: ICommand): ICommandBuilder; |
|
Manually set the command as argument. Mostly used for test purpose. |
function GetCommandsFound: Integer; |
|
Returns the number of commands found among the arguments passed as parameters after Parse. |
function GetDefaultCommand: ICommand; |
|
Returns the default command if one has been configured. |
function GetRawArguments: TArray<string>; |
|
Returns a list of raw arguments passed as parameters, including invalid arguments. |
function GetRawOptions: TArray<string>; |
|
Returns a list of raw options passed as parameters, including invalid options. |
function ExeName: string; |
|
Returns exe name that will be used as command line shell name to start the application. It´s used to outputs usage info. |
function HasCommands: Boolean; |
|
Returns True if CommandBuilder has at least one command configured. |
function HasArguments: Boolean; |
|
Returns True if CommandBuilder has at least one argument configured. |
function HasOptions: Boolean; |
|
Returns True if CommandBuilder has any option configured in any command. |
function UseArguments(AArguments: TArray<string>): ICommandBuilder; |
|
Allows to inject external arguments instead of reading from ParamStr(). Mainly used for testing purposes.
Parameters
|
function UseColorTheme(ATheme: TColorTheme): ICommandBuilder; |
|
Allows to use a different color theme for the too. Returns the CommandBuilder to allow the use of fluent interface.
Parameters
|
Properties
property Commands: TArray<ICommand> read GetCommands; |
|
Returns the list of commands configured in CommandBuilder. |
property Arguments: TArray<IArgument> read GetArguments; |
|
Returns the list of arguments configured in CommandBuilder. |
property ParsedOptions: TArray<IOption> read GetParsedOptions; |
|
Returns the list of valid options found after parse. |
property ParsedArguments: TArray<IArgument> read GetParsedArguments; |
|
Build a list of IArguments related to selected command, if there are more than one argument provided the list will match one argument parameter by order of the command builder configuration |
property InputLn: TInputLnCallback read GetInputLn write SetInputLn; |
|
Callback procedure used to capture user input. A default callback is provided by the library, but it can be overridden for testing purposes primarily. Should return user input. |
property Output: TOutputCallback read GetOutput write SetOutput; |
|
Callback procedure that is intended to output text to the console, or other desired output. The library provides a standard callback that simply calls WriteLn, but it can be overridden so that the output is redirected to a file, a test function, etc. |
property OutputColor: TOutputColorCallback read GetOutputColor write SetOutputColor; |
|
Callback procedure that is intended to output text to the console usings colors. The library provides a standard callback that simply calls Write before change console color, but it can be overridden so that the output is redirected to a file, a test function, etc. |
property ColorTheme: TColorTheme read GetColorTheme write SetColorTheme; |
|
Color theme that should be used to output colors of commands, a standard theme is provided by Command.Colors unit. Should be changed prior to application execution |
property Title: string read GetTitle write SetTitle; |
|
Application title, can be customized and is displayed as part usage info to the user through the command UsageCommand. |
property State: string read GetState write SetState; |
|
State that can be set by user commands allowing better communication between commands and the application. |
Generated by PasDoc 0.16.0.