Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

ParamParser

Minimal Delphi/Object pascal application execution parameters parser

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

ParamParser

A minimal command-line parameter parser for Delphi / Object Pascal applications.


Features

  • 🪶 One file, no dependencies. It only needs Classes, SysUtils and Math.
  • 🏳️ Flags: -d or --debug return 'true'.
  • 🧩 Valued parameters: -t value or --target value return 'value'.
  • 🔀 Short and long names: look up -t and --target in one call.
  • 🎯 Exact matching: -t does not match --test, and it does not match a value that contains -t.

Installation

  1. Copy paramparser.pas into your project folder, or add its folder to your unit search path.
  2. Add ParamParser to your uses clause.

[!NOTE] The unit does not declare a compiler mode. With Free Pascal / Lazarus, compile it in objfpc or delphi mode, either from the project options or with -Mobjfpc on the command line, so that Result is available.

Quick start

pascal
program Demo;

{$mode objfpc}{$H+}

uses
  Classes, ParamParser;

var
  Params: TStringList;
begin
  Params := TStringList.Create;
  try
    ParseParams(Params);

    WriteLn('Target: ', GetParam(Params, 't', 'target'));
    WriteLn('Debug:  ', GetParam(Params, 'd', 'debug'));
  finally
    Params.Free;
  end;
end.
bash
$ ./demo -t production --debug
Target: production
Debug:  true

$ ./demo --target staging
Target: staging
Debug:  false

API

ParseParams

pascal
procedure ParseParams(var ParamList: TStringList);

Reads the parameters passed to the program (ParamStr(1) to ParamStr(ParamCount)) and adds them to ParamList.

  • A parameter that starts with - or -- and is followed by a parameter that does not start with - is stored with its value, as -t|value.
  • Any other parameter that starts with - or -- is stored as a flag, as -d.
  • Parameters that do not start with - and do not follow a parameter name are ignored.

[!IMPORTANT] The caller creates ParamList before the call and frees it afterwards.

GetParam

pascal
function GetParam(var ParamList: TStringList; ParamName: String;
  LongParamName: String): String;

Looks up a parameter by its short name (-ParamName) or its long name (--LongParamName). Pass the names without the dashes.

Command line GetParam(Params, 't', 'target')
-t production 'production'
--target production 'production'
-t 'true'
(not passed) 'false'

The result is always a String. Convert it as needed, for example with StrToBool or StrToIntDef.

Behavior and limitations

  • A value cannot start with -. In -n -5, -5 is read as another parameter, so -n becomes a flag.
  • Combined short flags such as -abc are not split. -abc is a single parameter named abc.
  • The --name=value syntax is not supported. Use --name value.
  • Names are case-sensitive: -T and -t are different parameters.
  • If a parameter is passed more than once, GetParam returns the first one.

License

Distributed under the MIT License. © 2025 Rodrigo Peetz.