OptionParser... open source command line handling library

I have released a command line option parser for Xojo. It handles parsing command line parameters for Console, Desktop and Web applications. It will convert parameters to correct Xojo types, validate options, generate help, etc… This class was written by Kem Tekinay and myself and some string parsing code created by Michel Bujardet in (https://forum.xojo.com/14420-system-commandline/p1#p117101) .

For download information and API docs please see http://jcowgar.github.io/xojo-option-parser/

A quick example:

Function Run(args() As String) // Console App Example
  Dim opt As New OptionParser
  opt.AddOption New Option("v", "verbose", "Enable verbose output", Option.OptionType.Boolean)
  opt.AddOption New Option("f", "file", "File to parse", Option.OptionType.File)
  opt.AddOption New Option("c", "count", "Number of times to perform operation", Option.OptionType.Integer)
  opt.Parse(args) // System.CommandLine for GUI/Web

  If opt.BooleanValue("verbose") Then // could access as "v" as well
    Print "Verbosity is on!"
  End If

  Dim fh As FolderItem = opt.FileValue("file")
  For i As Integer = 1 To opt.IntegerValue("count")
    // Do work with fh
  Next

  Return 0
End Function

This was Jeremy’s concept, and it’s first-rate. If you want to include command-line parsing that is indistinguishable from “professional” apps, this class will do the job. Easily.

Nice work gentlemen!

looks like something I was going to write. thanks for writing it.

I just ran the Example.xojo_project, and I get an illegal cast exception in line 9 of the Get function of IsValid Property of Option in the folder OptionParser.
d is nil.

Output of Terminal seems normal :

Last login: Mon Aug 25 10:27:17 on ttys002 Mitchs-iMac:~ Mitch$ /Users/Mitch/Downloads/xojo\\-option\\-parser\\-master/Example/example.debug/example.debug --test --person=Jack -o Howdy -mnc 5 Initializing Test Groups... Running Tests...

Mac OS X 10.9.5, 2014R2.1

Years ago I wrote a similar class to mimic the GNU GetOpt library. It’s mostly abandoned, since I no longer used it, but perhaps can be of help for this parser.
Source code here: https://github.com/maxvalle/RBGetOpt

Michel,

Yes, that unit test drives me nuts. I’m not exactly sure how to handle it. The test itself is checking to make sure that validation works. Validation throws an exception. If you have Break on Exceptions turned on in the IDE, then it breaks at that point.

That exception is expected.

If you have a better way of how to do that particular test, I’d be all ears, as I said, it drives me nuts.

Now, you can see the option parser at work by removing the --test from the Command Line parameters. It will not run the unit tests. Or you could temporarily turn off “Break on Exceptions” in the IDE and run to see the unit tests pass.

[quote=124393:@Massimo Valle]Years ago I wrote a similar class to mimic the GNU GetOpt library. It’s mostly abandoned, since I no longer used it, but perhaps can be of help for this parser.
Source code here: https://github.com/maxvalle/RBGetOpt[/quote]

If it follows the GNU GetOpt convention, I was thinking of adding a QuickParse() or something to OptionParser for those that don’t need anything more complex. I’ll take a peek at your class, thanks for mentioning it here! What I was thinking was:

Dim d As Dictionary = OptionParser.QuickOpts("vc:f:o")

for example. Would validate options, but no validation of parameter values, no conversion to Xojo types, no help generation, etc… but a single line option parser! Now, for the LongOpt variant, I was thinking one could do:

Dim d As Dictionary = OptionParse.QuickOpts("vc:f:o", "verbose,challenge:,file:,output")

Not as much as the GNU LongOpts, but provides short and long options.

Or you can just do what explained here https://forum.xojo.com/15186-breakonexceptions to temporarily ignore the exception.

Doh! I forgot about BreakOnExceptions. I’ve added it to the validation unit test and pushed. All is well now, thanks for the help on that Massimo!

Is there a chance this gets updated to be 64 bit compatible for the declares?

I do not have any immediate plans to do so, but the code is on GitHub and would gladly merge a pull request with these updates. If I recall, there are not too many in there.

I guess the Cocoa declares only need a check for the datatypes but the Windows and Linux declares maybe need to use different functions… no idea.