Since I keep forgetting about this, and there is the Embarcadero documentation for it has had no examples added in Delphi 2007 or later: [WayBack] Delphi Basics : FindCmdLineSwitch command
Example code : Saerch the program parameters for 3 switches begin
// Before running this code, use the Run/parameters menu option
// to set the following command line parameters : /def abc /ghi
ShowMessage(CmdLine); // Show the execution command + parameters
// How many parameters were passed?
ShowMessage(‘There are ‘+IntToStr(ParamCount)+’ parameters’);
// Scan for parm1, parm2 and parm3 parameters
if FindCmdLineSwitch(‘abc’)
then ShowMessage(‘abc found’)
else ShowMessage(‘abc NOT found’);
if FindCmdLineSwitch(‘def’)
then ShowMessage(‘def found’)
else ShowMessage(‘def NOT found’);
if FindCmdLineSwitch(‘ghi’)
then ShowMessage(‘ghi found’)
else ShowMessage(‘ghi NOT found’);
end;
Show full unit code “C:\Program files\Borland\Delphi7\Projects\Project1.exe” /def abc /ghi
There are 3 parameters
abc NOT found
def found
ghi found
Related: [WayBack] delphi – How to read value of ParamStr with different deliminators? – Stack Overflow
Unchanged Embarcadero Delphi 2007 documentation:
- [WayBack] SysUtils.FindCmdLineSwitch Function
- [WayBack] System.ParamStr Function
- [WayBack] System.ParamCount Function
For comparison, the Delphi 10.2 Rio documentation:
- [WayBack] System.SysUtils.FindCmdLineSwitch – RAD Studio API Documentation
FindCmdLineSwitch determines whether a string represents a command-line switch, based on the first character of the string.
Switch
is a command-line parameter to search for.Chars
is the set of characters that distinguish switches from other parameters. IfSwitch
is omitted, it defaults to ‘-‘ on Unix based systems, and to ‘/’ and ‘-‘ on Windows based systems.IgnoreCase
determines whether case-insensitive character matching is used. The default is True. This function can also be used to retrieve parameter values in addition to finding command-line switches, for that overload.Value
returns the value associated with the specified switch.SwitchTypes
determines how parameter values are parsed:
-
-
-
Constant Value clstValueNextParam -p Value
clstValueAppended -p Value
or -p:Value
-
-
The
clstValueNextParam
parameter specifies that values are separated from the switch by a space character. TheclstValueAppended
parameter specifies that the values are appended immediately, following the switch (no space) or after a colon.SwitchTypes
is a set type parameter and the default is to allow values of both parameter types to be returned–[clstValueNextParam, clstValueAppended]. Any single switch type may be specified as well. An emptySwitchType
parameter, [], will result in no values being returned. Pass theSwitchTypes
parameter to exclude any of these switch types (Switch
may be 1 or more characters in length.) - [WayBack] System.ParamCount – RAD Studio API Documentation
- [WayBack] System.ParamStr – RAD Studio API Documentation
- [WayBack] System.SysUtils.TCmdLineSwitchType – RAD Studio API Documentation
- [WayBack] System.SysUtils.TCmdLineSwitchTypes – RAD Studio API Documentation
- [WayBack] System.SysUtils.TSysCharSet – RAD Studio API Documentation
The really odd thing? The really versatile TCommandParser
class got ditched after Delphi XE6: On the Delphi TCommandParser class for parsing command-lines and arguments (via: Suggestions for how to define command line parameters – Stack Overflow).
–jeroen