Why another command line parser?

There are lots of command line parser out there, for example trollop is a great alternative. However, it's 800 lines long. In addition, trollop sucks at validating the input. So µ-optparse is for you if you are looking for

What is µ-optparse?

µ-optparse is a small wrapper around optparse, weighting less than 75 lines of code. optparse (or OptionParser) on the other hand is a command line parser, which ships with ruby. After you defined available options, it automatically creates a help page and is able to parse ARGV accordingly. However, optparse requires you to repeat yourself quite often, which leads to many lines of code, just to configure the available options. µ-optparse removes this obstacle by extracting the information contained in few lines of configuration. In addition, µ-optparse extends optparse by some powerful validations, which where inspired by OptiFlag.

Talk code!

        require 'rubygems' # necessary for ruby v1.8.*
        require 'micro-optparse'
        options = Parser.new do |p|
          p.banner = "This is a fancy script, for usage see below"
          p.version = "fancy script 0.0 alpha"
          p.option :severity, "set severity", :default => 4, :value_in_set => [4,5,6,7,8]
          p.option :verbose, "enable verbose output"
          p.option :mutation, "set mutation", :default => "MightyMutation", :value_matches => /Mutation/
          p.option :plus_selection, "use plus-selection if set", :default => true
          p.option :selection, "selection used", :default => "BestSelection", :short => "l"
          p.option :chance, "set mutation chance", :default => 0.8, :value_satisfies => lambda {|x| x >= 0.0 && x <= 1.0}

What this piece of code does is the following:

The automatically generated help message looks like this:

            This is a fancy script, for usage see below
                -s, --severity 4                 set severity
                -v, --[no-]verbose               enable verbose output
                -m, --mutation MightyMutation    set mutation
                -p, --[no-]plus-selection        use plus-selection if set
                -l, --selection BestSelection    selection used
                -c, --chance 0.8                 set mutation chance
                -h, --help                       Show this message
                -V, --version                    Print version

Where do I get µ-optparse?

You can either go and install the gem via gem install micro-optparse or grab it from github and paste it into your script. If you choose the latter option, you may delete the validate-method to spare another 15 lines of code.

If you want to contribute, you can fork the github repository, make your changes and send me a pull request. However, improvements must be one of the following:

Frequently Asked Questions

All my argument values are either true or false - what's wrong?

You must define default values, if the option should accept an argument. Every option without a default value (or with true or false as default) is treated as a switch: true if given and false / default otherwise.

Is it possible to define mandatory / required arguments, which must be provided?

No it's not. It should be possible in any case to provide a reasonable default value. If you come across a case where it's not possible, feel free to contact me.

Are long arguments with spaces and other special characters allowed?

Yes, just define an option which takes a String as an argument, i.e. pass a string as the default value for that option. Now everything between quotes will be parsed as the value for that argument, e.g. ruby testscript.rb --selection 'I want the best selection you have!!! And if possible, add Donuts.' Note that double quotes may cause trouble, for example I get an error if I use an exclamation mark in double quotes, but no error in single quotes.

Is it possible to define arguments which accept lists / arrays / multiple files / ... ?

Yes, just define an option which takes an Array as an argument, i.e. pass an array as the default value for that option. The input will be split by comma. If the arguments contain spaces, wrap the whole thing in single quotes or double quotes.

For example if you want to accept multiple file names with whitespaces in them:

        require 'rubygems' # necessary for ruby v1.8.*
        require 'micro-optparse'

        options = Parser.new do |p|
          p.option :filenames, "Files which will be processed", :default => []

        p options[:filenames]

ruby testscript.rb --filenames 'todo.txt,my great adventures.txt' yields ["todo.txt", "my great adventures.txt"].