Class TCommandLineReader
Unit
Declaration
type TCommandLineReader = class(TObject)
Description
A command line reader class that checks for valid arguments and automatically prints a formatted help.
Usage:
Declare all allowed arguments with the corresponding DeclareXXXX functions
Call parse to explicitely read the actual command line (optionally)
Use readXXX to read a declared argument
On the command line arguments can be given in different ways, e.g.
--name=value /name=value --name value /name value --name="value" /name="value"
Declared flags can be changed with –enable-flag
or –disable-flag
or –flag
where latter option negates the default value.
File names are checked for spaces, so it is not always necessary to surround them with quotes.
Internally, the allowed command line arguments are called both "properties" and "options".
Example:
var cmdline: TCommandLineReader; begin cmdline := TCommandLineReader.create; //Initial declaration of some command line parameters: cmdline.declareString('name', 'An example string property'); cmdline.declareString('foo', 'Another example string property', 'bar'); cmdline.declareInt('count', 'An example integer property', 123); cmdline.declareFlag('flag', 'An example boolean property'); //cmdline.parse(); //1. Parsing some command line options cmdline.parse('--name="some name" --count 9'); cmdline.readString('name'); //some name cmdline.readString('foo'); //bar cmdline.readInt('count'); //9 cmdline.readFlag('flag'); //false cmdline.existsProperty('name'); //true cmdline.existsProperty('foo'); //false //2. Parsing some other command line options cmdline.parse('--foo barbar --flag'); cmdline.readString('name'); // cmdline.readString('foo'); //barbar cmdline.readInt('count'); //123 cmdline.readFlag('flag'); //true cmdline.existsProperty('name'); //false cmdline.existsProperty('foo'); //true //3. Parsing some other command line options cmdline.parse('--help'); //prints automatically generated help to stdout and halts: { The following command line options are valid: --name=<string> An example string property --foo=<string> Another example string property (default: bar) --count=<int> An example integer property (default: 123) --flag An example boolean property } //4. Some more advanced usage cmdline.addAbbreviation('f'); //abbreviation for the last option (--flag) cmdline.allowDOSStyle := true; //DOS slash options. Default is only true on Windows cmdline.parse('/name "x y z" -f /count=1 /foo="abc"'); cmdline.readString('name'); //x y z cmdline.readString('foo'); //abc cmdline.readInt('count'); //1 cmdline.readFlag('flag'); //true cmdline.existsProperty('name'); //true cmdline.existsProperty('foo'); //true
Hierarchy
- TObject
- TCommandLineReader
Overview
Fields
language:TCommandLineReaderLanguage; |
|
onShowError: TCommandLineReaderShowError; |
|
showErrorAutomatically: boolean; |
|
allowDOSStyle: boolean; |
Methods
constructor create; |
|
destructor destroy; override; |
|
function availableOptions:string; |
|
procedure reset(); |
|
procedure parse(autoReset: boolean = true); overload; virtual; |
|
procedure parse(const s:string; skipFirst: boolean = false; autoReset: boolean = true); overload; virtual; |
|
procedure parse(const args:TStringArray; autoReset: boolean = true); overload; virtual; |
|
procedure beginDeclarationCategory(category: string); |
|
procedure declareFlag(const name,description:string;flagNameAbbreviation:char;default:boolean=false); overload; |
|
procedure declareFlag(const name,description:string;default:boolean=false); overload; |
|
procedure declareFile(const name,description:string;default:string=''); overload; |
|
procedure declareInt(const name,description:string;value: longint=0); overload; |
|
procedure declareString(const name,description:string;value: string=''); overload; |
|
procedure declareFloat(const name,description:string;value: extended=0); overload; |
|
procedure addAbbreviation(const abbreviation: char; const originalName: string = ''); |
|
procedure addEnumerationValues(const originalName: string; const enumeration: array of string); overload; |
|
procedure addEnumerationValues(const enumeration: array of string); overload; |
|
function readString(const name:string):string; overload; |
|
function readInt(const name:string):longint; overload; |
|
function readFloat(const name:string):extended; overload; |
|
function readFlag(const name:string):boolean; overload; |
|
function existsProperty(const name:string):boolean; |
|
function readNamelessFiles():TStringArray; |
|
function readNamelessString():TStringArray; |
|
function readNamelessInt():TLongintArray; |
|
function readNamelessFloat():TFloatArray; |
|
function readNamelessFlag():TBooleanArray; |
Properties
property onOptionRead: TOptionReadEvent read FOnOptionRead write FOnOptionRead; |
|
property onCustomOptionInterpretation: TOptionInterpretationEvent read FOnOptionInterpretation write FOnOptionInterpretation; |
|
property allowOverrides: boolean read FAllowOverrides write FAllowOverrides; |
Description
Fields
language:TCommandLineReaderLanguage; |
|
onShowError: TCommandLineReaderShowError; |
|
Event called when an invalid option is found during parsing of the command line. |
showErrorAutomatically: boolean; |
|
Automatically show an error message when the parsing finds an invalid option. |
allowDOSStyle: boolean; |
|
Allow |
Methods
constructor create; |
|
destructor destroy; override; |
|
function availableOptions:string; |
|
Returns a human-readable summary about the declared options. |
procedure reset(); |
|
Resets all options to their default values. |
procedure parse(autoReset: boolean = true); overload; virtual; |
|
Reads the standard command line parameters. |
procedure parse(const s:string; skipFirst: boolean = false; autoReset: boolean = true); overload; virtual; |
|
Reads the command line parameters from the string s. |
procedure parse(const args:TStringArray; autoReset: boolean = true); overload; virtual; |
|
Reads the command line parameters from the array args. |
procedure beginDeclarationCategory(category: string); |
|
Adds a new option category. The category is printed in the –help and availableOptions output |
procedure declareFlag(const name,description:string;flagNameAbbreviation:char;default:boolean=false); overload; |
|
DeclareFlag declares a boolean property. --enable-flag => flag:=true --disable-flag => flag:=false --flag => flag:=not default -xfy => flag:=not default (when flags x and y have also been declared) |
procedure declareFlag(const name,description:string;default:boolean=false); overload; |
|
Overloaded DeclareFlag without a single letter abbreviation. |
procedure declareString(const name,description:string;value: string=''); overload; |
|
|
procedure declareFloat(const name,description:string;value: extended=0); overload; |
|
|
procedure addEnumerationValues(const originalName: string; const enumeration: array of string); overload; |
|
Only allow certain values for property |
procedure addEnumerationValues(const enumeration: array of string); overload; |
|
Only allow certain values for the last property. |
function readString(const name:string):string; overload; |
|
Reads a previously declared string property. |
function readInt(const name:string):longint; overload; |
|
Reads a previously declared int property. |
function readFloat(const name:string):extended; overload; |
|
Reads a previously declared float property. |
function readFlag(const name:string):boolean; overload; |
|
Reads a previously declared boolean property. |
function existsProperty(const name:string):boolean; |
|
Tests if a declared property named name has been found on the command line. |
function readNamelessFiles():TStringArray; |
|
Reads all file names that are given on the command line and do not belong to a declared option (doesn't check for non existing files, yet). |
function readNamelessString():TStringArray; |
|
Reads all strings that are given on the command line and do not belong to a declared option. |
function readNamelessInt():TLongintArray; |
|
Reads all integers that are given on the command line and do not belong to a declared option. |
function readNamelessFloat():TFloatArray; |
|
Reads all floats that are given on the command line and do not belong to a declared option. |
function readNamelessFlag():TBooleanArray; |
|
Reads all booleans (true, false) that are given on the command line and do not belong to a declared option. |
Properties
property onOptionRead: TOptionReadEvent read FOnOptionRead write FOnOptionRead; |
|
Event called when an option has been parsed. (e.g. to read all values if an option is given multiple times) |
property onCustomOptionInterpretation: TOptionInterpretationEvent read FOnOptionInterpretation write FOnOptionInterpretation; |
|
Event called when an option is being parsed. (e.g. to allow custom abbreviations of names). |
property allowOverrides: boolean read FAllowOverrides write FAllowOverrides; |
|
If the same option may be given multiple times. Only the last value is stored. |
Generated by PasDoc 0.16.0.