Command Line Subcommands in Pascal

Here’s the translation of the Go code to Pascal, formatted in Markdown suitable for Hugo:

Our first program demonstrates how to work with command-line subcommands. Each subcommand can have its own set of flags, similar to how tools like git operate with subcommands like git commit or git push.

program CommandLineSubcommands;

uses
  SysUtils, Classes;

type
  TSubCommand = (scFoo, scBar, scUnknown);

var
  FooEnable: Boolean = False;
  FooName: string = '';
  BarLevel: Integer = 0;

function ParseCommandLine: TSubCommand;
var
  i: Integer;
begin
  Result := scUnknown;
  
  if ParamCount < 1 then
    Exit;
  
  if ParamStr(1) = 'foo' then
  begin
    Result := scFoo;
    for i := 2 to ParamCount do
    begin
      if ParamStr(i) = '-enable' then
        FooEnable := True
      else if Copy(ParamStr(i), 1, 6) = '-name=' then
        FooName := Copy(ParamStr(i), 7, Length(ParamStr(i)));
    end;
  end
  else if ParamStr(1) = 'bar' then
  begin
    Result := scBar;
    for i := 2 to ParamCount do
    begin
      if Copy(ParamStr(i), 1, 7) = '-level=' then
        BarLevel := StrToIntDef(Copy(ParamStr(i), 8, Length(ParamStr(i))), 0);
    end;
  end;
end;

procedure ExecuteFoo;
var
  i: Integer;
begin
  WriteLn('subcommand ''foo''');
  WriteLn('  enable: ', FooEnable);
  WriteLn('  name: ', FooName);
  Write('  tail: ');
  for i := 2 to ParamCount do
  begin
    if (ParamStr(i) <> '-enable') and (Copy(ParamStr(i), 1, 6) <> '-name=') then
      Write(ParamStr(i), ' ');
  end;
  WriteLn;
end;

procedure ExecuteBar;
var
  i: Integer;
begin
  WriteLn('subcommand ''bar''');
  WriteLn('  level: ', BarLevel);
  Write('  tail: ');
  for i := 2 to ParamCount do
  begin
    if Copy(ParamStr(i), 1, 7) <> '-level=' then
      Write(ParamStr(i), ' ');
  end;
  WriteLn;
end;

begin
  case ParseCommandLine of
    scFoo: ExecuteFoo;
    scBar: ExecuteBar;
    else
      WriteLn('expected ''foo'' or ''bar'' subcommands');
      Halt(1);
  end;
end.

This Pascal program implements a basic command-line interface with two subcommands: ‘foo’ and ‘bar’. Each subcommand has its own set of flags.

To compile and run the program:

$ fpc command_line_subcommands.pas
$ ./command_line_subcommands foo -enable -name=joe a1 a2
subcommand 'foo'
  enable: TRUE
  name: joe
  tail: a1 a2 

$ ./command_line_subcommands bar -level=8 a1
subcommand 'bar'
  level: 8
  tail: a1 

Note that Pascal doesn’t have built-in flag parsing like Go’s flag package, so we’ve implemented a simple parsing mechanism. In a real-world application, you might want to use a more robust command-line parsing library for Pascal.

Also, Pascal doesn’t have a direct equivalent to Go’s flag.NewFlagSet, so we’ve used a case statement to handle different subcommands. The parsing of command-line arguments is done manually in the ParseCommandLine function.

This example demonstrates how to create a basic command-line tool with subcommands in Pascal, adapting the concepts from the original Go code to Pascal’s syntax and standard library.