The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,230 other subscribers

Archive for May 13th, 2015

On the Delphi TCommandParser class for parsing command-lines and arguments (via: Suggestions for how to define command line parameters – Stack Overflow)

Posted by jpluimers on 2015/05/13

A while ago, I posted [WayBack] the below StackOverflow answer. Recently a friend asked me about command-line parsing in Delphi, so here is the re-run:

Delphi contains a really nice unit called CommandParser with a [WayBack] TCommandParser class that does commandline parsing for you. Since it has [WayBack] virtually no documentation, here are a few things to get started. It works even in Delphi 2007.

I have a HiddenExecutable example at our open source bo repository.

Basically you:

  • TComponent that contains the properties you want to expose as commandline parameters (that is THiddenExecuteSettings in the HiddenExecuteSettingsUnit
  • a commandline parser controller. In our case THiddenExecuteArguments in theTHiddenExecuteArgumentsUnit (in retrospect not such a good name) that contains an InitCommandLine method that sets up a TCommandParser instance passing it your TComponent
  • It then executes a couple of [WayBack] AddSwitch calls to setup the parameters with both abbreviated and full commandline switches (like h and help)
  • ProcessCommandLine method on the TCommandParser instance to process the commandline and fill the properties of your TComponent (in my example, this is done in the ProcessCommandLine method).

Now comes the fun:

  • TCommandParser has a HelpText method that fully automatically assembles a helptext based upon what you fed it with the AddSwitch methods.
  • TCommandParser also has a SaveOptions method that allows you to save the current settings of your TComponent into a settings file.

The Delphi units you need are these which you can get from the Embarcadero [WayBack] demo repository:

CommandParser in '...\\branches\RadStudio_XE2\Delphi\Database\dbExpress\Utils\CommandParser.pas',
PropertyHelpers in '...\\branches\RadStudio_XE2\Delphi\Database\dbExpress\DbxDataPump\PropertyHelpers.pas',
ParseIds in '...\\branches\RadStudio_XE2\Delphi\Database\dbExpress\DbxDataPump\ParseIds.pas',

Edit: [WayBack] John Kaster wrote a nice [WayBack] article on EDN that includes [WayBack] more details on using the TCommandParser.

Note the above mentioned code mostly is on [WayBack]

Since Delphi XE7, this unit does not ship with Delphi any more, but it is still at [WayBack] RAD Studio Demo Code / Code / [r2029] /branches/RadStudio_XE6/Object Pascal/Database/dbExpress/Utils/CommandParser.pas.


via: [WayBack] delphi – Suggestions for how to define command line parameters – Stack Overflow.

Posted in Delphi, Delphi 2007, Delphi 2009, Delphi 2010, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Delphi XE7, Development, Software Development | 4 Comments »

%d bloggers like this: