Article Preview
Buy Now
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) orSystem.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 newOptionsParser
object. The OptionsParser is the core and does all the heavy lifting.
Dim o As Option
is where we declare a newOption
object. BothOptions.AddOption New Option("v", "verbose", "turn on verbose output mode", Option.OptionType.Boolean)
and theo = 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 theo
object before adding it toOptions
.Next we will parse the
args
(System.commandline
in Desktop and Web Edition applications) inside aTry/Catch
block. Using theTry/Catch
block catches any errors since OptionsParser throws exceptions. The lineOptions.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.