Skip to content

Instantly share code, notes, and snippets.

@jarroddavis68
Last active November 8, 2022 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarroddavis68/06934f2b9ab00ba3ff5e0d49a061ede0 to your computer and use it in GitHub Desktop.
Save jarroddavis68/06934f2b9ab00ba3ff5e0d49a061ede0 to your computer and use it in GitHub Desktop.
TCmdLine - Enhanced Command line Processing for Delphi
{==============================================================================
TCmdLine - Enhanced Command line Processing for Delphi
Copyright © 2022 tinyBigGAMES™ LLC
All Rights Reserved.
Website: https://tinybiggames.com
Email : support@tinybiggames.com
Redistribution and use in source and binary forms, with or without
modifications are permitted provided that the following conditions are met:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
3. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
4. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
5. All video, audio, graphics, and other content accessed through the
software in this distro is the property of the applicable content owner
and may be protected by applicable copyright law. This License gives
Customer has no rights to such content, and Company denies any liability
for misuse of content.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
============================================================================= }
unit CmdLine;
interface
uses
System.Types,
System.SysUtils,
System.Math,
WinApi.Windows;
type
{ TCmdLine }
TCmdLine = class
private
FCmdLine: string;
FParamStr: TStringDynArray;
procedure Build;
public
constructor Create;
destructor Destroy; override;
procedure Reset;
procedure Clear;
procedure AddParam(const aParam: string);
procedure AddParams(const aParams: array of string);
function Str: string;
function Count: Integer; overload;
function Count(const aName: string): Integer; overload;
function Param(const aIndex: Integer): string; overload;
function Param(const aName: string; const aIndex: Integer): string; overload;
function ParamIndex(const aName: string): Integer;
function ParamExist(const aName: string): Boolean;
function ParamValue(const aName: string; var aValue: string): Boolean;
end;
implementation
{ TCmdLine }
procedure TCmdLine.Build;
var
I: Integer;
begin
FParamStr := nil;
FParamStr := FCmdline.Split([' '],'"', '"', TStringSplitOptions.ExcludeEmpty);
for I := Low(FParamStr) to High(FParamStr) do
begin
FParamStr[I] := FParamStr[I].DeQuotedString;
FParamStr[I] := FParamStr[I].DeQuotedString('"');
FParamStr[I] := FParamStr[I].DeQuotedString('`');
end;
FParamStr[0] := ParamStr(0);
end;
constructor TCmdLine.Create;
begin
inherited;
Reset;
end;
destructor TCmdLine.Destroy;
begin
inherited;
end;
procedure TCmdLine.Reset;
begin
FCmdLine := CmdLine;
Build;
end;
procedure TCmdLine.Clear;
begin
FCmdLine := ParamStr(0);
Build;
end;
procedure TCmdLine.AddParam(const aParam: string);
begin
FCmdLine := FCmdLine + ' ' + aParam;
Build;
end;
procedure TCmdLine.AddParams(const aParams: array of string);
var
LParam: string;
begin
for LParam in aParams do
begin
AddParam(LParam);
end;
end;
function TCmdLine.Str: string;
begin
Result := FCmdLine;
end;
function TCmdLine.Count: Integer;
begin
Result := Length(FParamStr)-1;
end;
function TCmdLine.Count(const aName: string): Integer;
var
LIndex: Integer;
begin
Result := 0;
LIndex := ParamIndex(aName);
if LIndex = -1 then Exit;
Result := Count - LIndex;
end;
function TCmdLine.Param(const aIndex: Integer): string;
begin
Result := '';
if (aIndex < 0) or (aIndex > Count-1) then Exit;
Result := FParamStr[aIndex];
end;
function TCmdLine.Param(const aName: string; const aIndex: Integer): string;
var
LIndex: Integer;
begin
Result := '';
LIndex := ParamIndex(aName);
if LIndex = -1 then Exit;
Inc(LIndex, aIndex);
if not InRange(LIndex, Low(FParamStr), High(FParamStr)) then Exit;
Result := FParamStr[LIndex];
end;
function TCmdLine.ParamIndex(const aName: string): Integer;
var
I: Integer;
begin
Result := -1;
for I := Low(FParamStr) to High(FParamStr) do
begin
if SameText(FParamStr[I], aName) then
begin
Result := I;
Exit;
end;
end;
end;
function TCmdLine.ParamExist(const aName: string): Boolean;
var
LParam: string;
begin
Result := False;
for LParam in FParamStr do
begin
// check if param exist
if SameText(LParam, aName) then
begin
Result := True;
Exit;
end;
end;
end;
function TCmdLine.ParamValue(const aName: string; var aValue: string): Boolean;
var
LParam: string;
begin
Result := False;
aValue := '';
for LParam in FParamStr do
begin
// check if param exist
if SameText(LParam, aName) then
begin
Result := True;
Exit;
end;
// check for switch exits
if LParam.StartsWith('/' + aName, True) or
LParam.StartsWith('-' + aName, True) then
begin
// check if switch as a value
if LParam.Contains(':') then
begin
aValue := LParam.Substring(LParam.IndexOf(':') + 1);
end;
Result := True;
Exit;
end;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment