DelphiBasics
FindCmdLineSwitch
Function
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;
Description
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
CmdLine Holds the execution text used to start the current program
ParamCount Gives the number of parameters passed to the current program
ParamStr Returns one of the parameters used to run the current program
TSysCharSet Characters used by supplied string parsing functions
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Saerch the program parameters for 3 switches
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FindCmdLineSwitch command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

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;
 
end.
Hide 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 2002 - 2017. All rights reserved.  |  Home Page