Path: README.txt
Last Update: Fri Oct 02 09:09:19 -0700 2009



Yeah yeah yeah. Why in heaven‘s name do we need yet another command-line parser? Well, OptionParser is all well and good[1], but doesn‘t grease the skids as much as I‘d like. Simple things should be dead simple (1 LOC), and more flexibility is there if you need it.



You like command-line parsing, but you hate all of the bloat. Why should you have to create a Hash, then create a parser, fill the Hash out then throw the parser away (unless you want to print out a usage message) and deal with a Hash? Why, for Pete‘s sake, should the parser and the parsed values be handled by two different objects?

Introducing Clip


And it goes a little something like this…

  require "rubygems"
  require "clip"

  options = Clip do |p|
    p.optional 's', 'server', :desc => 'The server name', :default => 'localhost'
    p.optional 'p', 'port', :desc => 'The port', :default => 8080 do |v|
      v.to_i # always deal with integers
    p.required 'f', 'files', :multi => true, :desc => 'Files to send' do |files|
      files.each do |f|
        raise("unable to read file #{f}") unless File::readable? f
    p.flag 'v', 'verbose', :desc => 'Make it chatty'

  if options.valid?
    if options.verbose?
      puts options.server if options.server?
      puts options.port
      puts 'files:'
      options.files.each do |f|
        puts "\t#{f}"
    # print error message(s) and usage
    $stderr.puts options.to_s

The names of the options and flags that you declare in the block are accessible as methods on the returned object, reducing the amount of objects you have to deal with when you‘re parsing command-line parameters.

You can optionally process parsed arguments by passing a block to the required or optional methods which will set the value of the option to the result of the block. The block will receive the parsed value and should return whatever transformed value that is appropriate to your use case. If the passed value fails validation you can raise an error which will be reported correctly.

Simply invoking the to_s method on a parser instance will dump both the correct usage and any errors encountered during parsing. No need for you to manage the state of what‘s required and what isn‘t by yourself. Also, ’—help’ and ’-h’ will automatically trigger Clip to dump out usage and exit.

Sometimes you have additional arguments you need to process that don‘t require a named option or flag. Whatever remains on the command line that doesn‘t fit either a flag or an option/value pair will be made available via the remainder method of the returned object.

Sometimes even passing a block is overkill. Say you want to grab just a hash from a set of name/value argument pairs provided:

  $ my_clip_script subcommand -c config.yml # Allows:
  Clip.hash == { 'c' => 'config.yml' }

  $ my_clip_script -c config.yml --mode optimistic # Allows:
   Clip.hash == { 'c' => 'config.yml', 'mode' => 'optimistic' }