Determine whether a certain parameter switch was passed SysUtils unit
1 function FindCmdLineSwitch(const SwitchValue string):Boolean;
2 function FindCmdLineSwitch (const SwitchValue string; IgnoreCase Boolean:Boolean;
3 function FindCmdLineSwitch (const SwitchValue string; SwitchChars TSysCharSet; IgnoreCase Boolean:Boolean;
The FindCmdLineSwitch function scans the command line (program + parms used to invoke the current program) for a given parameter switch. It saves the coder from this parsing activity.
Switches are optional parameters prefixed by :
- / for Windows - for Linux
but may be specified in SwitchChars in Version 3 of the function.
The SwitchValue follows this prefix character, and is the value scanned for in the command line by this function.
The IgnoreCase parameter in Versions 2, 3 of the function determines whether the scan ignores the case of the SwitchValue. The default is True
Related commands
CmdLineHolds the execution text used to start the current program
ParamCountGives the number of parameters passed to the current program
ParamStrReturns one of the parameters used to run the current program
TSysCharSetCharacters used by supplied string parsing functions
 Download this web site as a Windows program.

Example code : Saerch the program parameters for 3 switches
  // 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');
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
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page