{*******************************************************}
{                                                       }
{       Tocsg.Param                                     }
{                                                       }
{       Copyright (C) 2020 kkuzil                       }
{                                                       }
{*******************************************************}

unit Tocsg.Param;

interface

uses
  Tocsg.Obj, System.SysUtils;

type
  TTgParam = class(TTgObject)
  private
    nParamCnt_: Integer;
    sModulePath_,
    sModuleName_: String;
  protected
    function GetParamIndex(sParam: String): Integer;
  public
    Constructor Create; override;

    function ExistsParam(const sParam: String): Boolean;
    function GetParamValue(const sParam: String): String;

    property ModulePath: String read sModulePath_;
    property ModuleName: String read sModuleName_;
    property Count: Integer read nParamCnt_;
  end;

implementation

{ TTgParam }

Constructor TTgParam.Create;
begin
  Inherited Create;

  nParamCnt_ := ParamCount;
  sModulePath_ := ParamStr(0);
  sModuleName_ := ExtractFileName(sModulePath_);
end;

function TTgParam.GetParamIndex(sParam: String): Integer;
var
  i: Integer;
begin
  Result := -1;

  sParam := UpperCase(sParam);
  for i := 1 to nParamCnt_ do
    if UpperCase(ParamStr(i)) = sParam then
    begin
      Result := i;
      exit;
    end;
end;

function TTgParam.ExistsParam(const sParam: String): Boolean;
begin
  Result := GetParamIndex(sParam) > -1;
end;

function TTgParam.GetParamValue(const sParam: String): String;
var
  nIdx: Integer;
begin
  Result := '';
  nIdx := GetParamIndex(sParam);
  if (nIdx >= 1) and (nIdx + 1 <= nParamCnt_) then
    Result := ParamStr(nIdx + 1);
end;

end.
