VSoft Technologies Blogs

rss

VSoft Technologies Blogs - posts about our products and software development.

A while back I published the VSoft.CommandLineParser library on github, which makes it simple to handle command line options in delphi applications. The first version only did enough to satisfy the needs I had in DUnitX.

In another project I’m working on, I needed a command mode, where each command had a unique set of options, but keeping the ability to have global options.  I have tried to implement the command mode in a backwards compatable manner, and so far the only change I had to make to an existing project was adding a const to a parameter.

Adding Commands

Adding commands is quite simple, using TOptionsRegistry.RegisterCommand.

cmd := TOptionsRegistry.RegisterCommand('help','h','get some help','','commandsample help [command]'); 
option := cmd.RegisterUnNamedOption<string>('The command you need help for', 
  procedure(const value : string) 
  begin 
    THelpOptions.HelpCommand := value; 
  end);

Note: this method returns a TCommandDefinition record that you can add options to. The reason for using a record rather than an interface here, is because delphi interfaces do not suport generic methods. Records do, so we use the record type as a wrapper around the ICommandDefinition interface.

The helpstring parameter allows you to specify a longer help message that can be displayed when showing command usage.

Handling Commands

The ICommandLineParseResult interface has a new Command property (string) which is used to determine the selected command. It’s up to you how to actually run the commands.

Showing Usage

The PrintUsage method now has some overloads and has some formatting improvements, and TOptionsRegistry also has new EnumerateCommands and EmumerateCommandOptions methods which make it relatively simple to handle showing usage etc yourself if you want to.

Where is it?

The source with samples is available on GitHub - https://github.com/VSoftTechnologies/VSoft.CommandLineParser

Showing 0 Comment


Comments are closed.