Mega Code Archive

 
Categories / Delphi / Examples
 

Getting parameter values - updated

Getting parameter values with delphi is extremely easy, you have the ParamStr and ParamCount functions to work with, also there is FindCmdLineSwitch function which will tell you if a certain parameter is being used. however if you use a parameter like /paramname:paramvalue then the following function will be useful when getting these parameter values. (* GetParameterValue GetParameterValue will return the value associated with a parameter name in the form of /paramname:paramvalue -paramname:paramvalue and /paramname -paramname ParamName - Name of the parameter (paramname) SwitchChars - Parameter switch identifiers (/ or -) Seperator - The char that sits between paramname and paramvalue (:) Value - The value of the parameter (paramvalue) if it exists Returns - Boolean, true if the parameter was found, false if parameter does not exist typical usage Parameter -P=c:\temp\ -S GetParameterValue('p', ['/', '-'], '=', sValue); sValue will contain c:\temp\ *) function GetParameterValue(const ParamName: string; SwitchChars: TSysCharSet; Seperator: Char; var Value: string): Boolean; var I, Sep: Longint; S: string; begin Result := False; Value := ''; for I := 1 to ParamCount do begin S := ParamStr(I); if Length(S) > 0 then if S[1] in SwitchChars then begin Sep := Pos(Seperator, S); case Sep of 0: begin if CompareText(Copy(S, 2, Length(S) -1), ParamName) = 0 then begin Result := True; Break; end; end; 1..MaxInt: begin if CompareText(Copy(S, 2, Sep -2), ParamName) = 0 then begin Value := Copy(S, Sep + 1, Length(S)); Result := True; Break; end; end; end; //case end; end; end; updated on 27 Nov 03