Get command line parameters

(* 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 := sBlank;

  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;

 

Share this article!

Follow us!

Find more helpful articles: