Einzelnen Beitrag anzeigen

Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#3

AW: unit für command line parameter

  Alt 17. Okt 2019, 09:53
Wobei - Und das ist nur mein persönlicher Geschmack - ich denke dass man so etwas schneller selbst gebaut und auf seine eigenen Bedürfnisse zugeschnitten hat als sich in eine neue Library eingearbeitet, Lizenztexte gelesen und verstanden hat und und und.

Delphi-Quellcode:
program Project9;

{$APPTYPE CONSOLE}
{$R *.res}

uses System.SysUtils;

type
   TCommandLineArgs = record
      isVerbose:   Boolean;
      inputPath:   String;
      outputPath:   String;

      function ToString(): String; inline;
   end;

function getCommandLineArgs(const commandLine: String): TCommandLineArgs;
begin
   Result.isVerbose := FindCmdLineSwitch('isVerbose');
   if not FindCmdLineSwitch('inputPath', Result.inputPath) then
      Result.inputPath := String.Empty;
   if not FindCmdLineSwitch('outputPath', Result.outputPath) then
      Result.outputPath := String.Empty;
end;

{ TCommandLineArgs }

function TCommandLineArgs.ToString(): String;
begin
   Result :=
      String.Format('isVerbose: %d', [isVerbose.ToInteger()])
      + sLineBreak + String.Format('inputPath: "%s"', [inputPath])
      + sLineBreak + String.Format('outputPath: "%s"', [outputPath]);
end;

begin
   WriteLn( getCommandLineArgs(CmdLine).ToString() );
end.
ergibt

Code:
isVerbose: 1
inputPath: ""
outputPath: "C:\users\public\someFile.dat"
bei einem Aufruf mit
Code:
/isVerbose /outputpath:C:\users\public\someFile.dat
Für nicht allzu komplizierte Fälle reicht so etwas doch schon aus?
  Mit Zitat antworten Zitat