Class TParameters
Unit
Declaration
type TParameters = class(TCastleStringList)
Description
Storing and processing command-line parameters and options. For simple processing, you can just read values of this list, checking count with comfortable methods like CheckHigh. For more involved processing, use the Parse function, that does a whole job for you based on a simple specification of allowed options.
Some terminology:
- Parameter
Command-line parameters list is given directly by the OS to our program. These are the contents of this list, initialized from the standard Pascal ParamStr/ParamCount. They can be modified to remove the already-handled parameters.
- Option
Options are things encoded by the user in the parameters. Examples:
Command-line
castle-model-viewer --navigation Walk
passes two parameters (
--navigation
andWalk
) for castle-model-viewer, and these two parameters form one option:--navigation=Walk
.Command-line
castle-model-viewer -hv
passes one parameter (
-hv
) for castle-model-viewer, and inside this parameter two options are encoded:-h
and-v
.
The very idea of this unit is to decode "options" from the "parameters".
- Argument
Argument is a part of the option, that clarifies what this option does. For example in
--navigation=Walk
, "Walk
" is the argument and "--navigation
" is the option long name.Some options don't take any arguments, some take optional argument, some take required argument, some have a couple of arguments. TOptionArgument type allows you to specify all this.
For simple programs, you can directly parse command-line by looking at our parameters strings. For more involved cases, using Parse method has a lot of advantages:
Less error-prone, and your program's code stays simple.
We automatically handle special parameter -- that is a standard way to mark the end of the options. (Useful for users that have filenames that start with "-" character.)
We automatically detect and make exceptions with nice messages on various errors. For example unrecognized options are clearly reported (so they will not mistaken for e.g. missing filenames by your program).
We automatically allow combining of short options, so user can use
-abc
instead of-a -b -c
.We have a simple interface, where you simply specify what options you want, long and short option names, option arguments and such.
See [https://castle-engine.io/common_options.php] for a user description how short and long options are expected to be given on the command-line.
Hierarchy
- TObject
- TPersistent
- TStrings
- TStringList
- TCastleStringList
- TParameters
Overview
Methods
function High: Integer; |
|
procedure CheckHigh(ParamValue: integer); |
|
procedure CheckHighAtLeast(ParamValue: integer); |
|
procedure CheckHighAtMost(ParamValue: integer); |
|
function IsPresent(const A: array of string): boolean; |
|
procedure Parse(const AOptions: POption_Array; const OptionsCount: Integer; const OptionProc: TOptionProc; const OptionProcData: Pointer; const ParseOnlyKnownOptions: boolean = false); overload; |
|
procedure Parse(const AOptions: array of TOption; const OptionProc: TOptionProc; const OptionProcData: Pointer; const ParseOnlyKnownOptions: boolean = false); overload; |
Description
Methods
function High: Integer; |
|
This item has no description. |
procedure CheckHigh(ParamValue: integer); |
|
Does the number of parameters (High) satisfy given condition. Exceptions raised
|
procedure CheckHighAtLeast(ParamValue: integer); |
|
This item has no description. |
procedure CheckHighAtMost(ParamValue: integer); |
|
This item has no description. |
function IsPresent(const A: array of string): boolean; |
|
Is one of given strings present on the parameters list. Looks inside Strings[1..High], case sensitive. |
procedure Parse(const AOptions: POption_Array; const OptionsCount: Integer; const OptionProc: TOptionProc; const OptionProcData: Pointer; const ParseOnlyKnownOptions: boolean = false); overload; |
|
Parse command-line parameters. Given a specification of your command-line options (in AOptions), we will find and pass these options to your OptionProc callback. The handled options will be removed from the Parameters list. After running this, you should treat the remaining Parameters as "normal" parameters, usually a filenames to open by your program or such. See also TOption for a specification of an option, and see TOptionArgument for a specification of an option argument, and see TOptionProc for a specification what your OptionProc callback gets.
Note that a single dash parameter is left alone, without making any exceptions, as this is a standard way of telling "standard input" or "standard output" for some programs. Note that a double dash parameter -- is handled and removed from the Parameters list, and signals an end of options. You should not modify Parameters list when this function is running, in particular do not modify it from your OptionProc callback. Also, do not depend on when the handled options are exactly removed from the Parameters list (before or after OptionProc callback). We never touch here the Strings[0] value, we look only at the Strings[1] to Strings[High]. ParseOnlyKnownOptions =
The ParseOnlyKnownOptions = Note that ParseOnlyKnownOptions = Exceptions raised
|
procedure Parse(const AOptions: array of TOption; const OptionProc: TOptionProc; const OptionProcData: Pointer; const ParseOnlyKnownOptions: boolean = false); overload; |
|
This item has no description. |
Generated by PasDoc 0.16.0-snapshot.