Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 14 in printed book and digital formats -- plus a one-year subscription so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 13.6 ('Stay Out of Jail')
Instant purchase and download via GumRoad!

COLUMN

Column

Issue: 13.6 (November/December 2015)
Author: Scott Boss
Author Bio: Scott Boss is the founder of Nocturnal Coding Monkeys, LLC, who specialize in writing custom software. Scott has been a developer, system administrator, storage engineer, consultant, and architect to businesses from startup to global 100.
Article Description: No description available.
Article Length (in bytes): 5,809
Starting Page Number: 76
Article Number: 13610
Related Link(s): None

Excerpt of article text...

This issue we will be looking at OptionParser (https://github.com/jcowgar/xojo-option-parser). This is for when you want your applications to accept parameters. The parameters don't have to be required, unless you want them to. I use OptionParser in all my applications now, to give me the ability to run the XojoUnit tests whenever I want to.

Now, instead of using OptionParser, you could parse args (console applications) or System.CommandLine (desktop and web edition applications) manually yourself. And, depending on how many parameters' options you are going to accept, this can be a very tedious task. Personally, I don't like reinventing the wheel when I don't have to. This is where OptionParser comes to the rescue.

Lets take a look at the code in Figure 1 line by line. First we have Options = New OptionParser("xdev", "example application for xDev.") This is where we are declaring a new OptionsParser object. The OptionsParser is the core and does all the heavy lifting.

Dim o As Option is where we declare a new Option object. Both Options.AddOption New Option("v", "verbose", "turn on verbose output mode", Option.OptionType.Boolean) and the o = New Option("u", "user", "user to run application as.") lines are defining new options that the parser will accept. The first one is used to define an option with no properties. This example option is a boolean flag setting the verbose mode of the application. Using the second method, allows us to define more properties to the option. It might be a required option, or it might take a range of numbers. When using a range, you have to define the upper and lower boundaries. Using the second method, make sure you make all the changes to the o object before adding it to Options.

Next we will parse the args (System.commandline in Desktop and Web Edition applications) inside a Try/Catch block. Using the Try/Catch block catches any errors since OptionsParser throws exceptions. The line Options.Parse(args) parses the arguments and sets everything up. That one line does all the heavy lifting.

We are defining the value of a global property by using this code app.verbose = Options.BooleanValue( "verbose" ). We made this property a global as we want to know if we are in verbose mode or not anywhere in the application. Instead of making a global we could have assigned the value to a property of a module. The rest of the code is designed to be based on the other options you may or may not pass to the application.

...End of Excerpt. Please purchase the magazine to read the full article.