[boo]
Mono.GetOptions rules
[ bamboo ] 18:29, Friday, 3 September 2004

I had to create a little (because boo code tends to be terse) console utility for a customer. The utility had to deal with lots of different command line options, that kind of thing. So I saw it as my opportunity to finally meet Mono.GetOptions.
What a nice tool!

import Mono.GetOptions from Mono.GetOptions

class CommandLineOptions(Options):

    def constructor(argv):
        ProcessArgs(argv)

    [Option("thumbnail width", "width")]
    public Width = 70

    [Option("thumbnail height", "height")]
    public Height = 70

    [Option("output file", "output")]
    public OutputFileName = ""

    [Option("input file", "input")]
    public InputFileName = ""

    [Option("encoding quality level (1-100), default is 75",  "encoding-quality")]
    public EncodingQuality = 75L

    IsValid as bool:
        get:
            return (0 == len(RemainingArguments) and
                        len(OutputFileName) > 0 and
                        len(InputFileName) > 0 and
                        Width > 0 and
                        Height > 0)

options = CommandLineOptions(argv)
if options.IsValid:	
    for field in typeof(CommandLineOptions).GetFields():
        print("${field.Name}: ${field.GetValue(options)}")
else:
    options.DoHelp()
Fire this script with
booi GetOptions.boo
and you'll already see a pretty professional looking help.
My heart would be filled of joy every time I needed to add support for a new parameter :)

Very nice work Rafael!

TrackBack
Comments

You're welcome Rodrigo.

Mono.GetOptions started as some explicit-table getopt look-alike. But while using C#/CLI I felt that resorting to reflection and metadata (Attributes) seemed as the best approach to get the "oh, that's is really simple" acknownledgement from the initial developers and specially for the long-term mantainers of client applications of the library.

Enjoy,

--Rafael Teixeira aka Monoman, September 23, 2004 11:56 AM
Post a comment









Remember personal info?