subcommand - subcommands using OptionParser

Wrapper over the awesome OptionParser which allows for easy and elegant subcommand definition, parsing and help generation. You don't have to sacrifice the goodness of OptionParser for the sake of subcommands and sub-options.

Links

Installation

sudo gem install subcommand

Usage example

if a program has subcommands foo and baz

     ruby subcommand.rb help
     ruby subcommand.rb --help
     ruby subcommand.rb help foo
     ruby subcommand.rb foo --help
     ruby subcommand.rb baz --quiet "some text"
     ruby subcommand.rb --verbose foo --force file.zzz

Sample output (trimmed)

    $ ruby subcommand.rb help

    Usage: subcommand.rb [options] [subcommand [options]]
    Stupid program that does something

    Global options are:
        -v, --[no-]verbose               Run verbosely

    Commands are:
       foo : desc for foo
       baz : desc for baz

    Aliases:
       goo - foo

    See 'subcommand.rb help COMMAND' for more information on a specific command.

    $ ruby subcommand.rb help foo

    Usage: foo [options]
    desc for foo
        -f, --[no-]force                 force action

 

STEPS

    1. define global_options (optional)

     global_options do |opts|
       opts.banner = "Usage: subcommand.rb [options] [subcommand [options]]"
       opts.description = "Stupid program that does something"
       opts.separator ""
       opts.separator "Global options are:"
       opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
         options[:verbose] = v
       end
     end

    2. define commands using command(). Send multiple names for aliases.

     command :foo do |opts|
       opts.banner = "Usage: foo [options]"
       opts.description = "desc for foo"
       opts.on("-f", "--[no-]force", "force action") do |v|
         options[:force] = v
       end
     end

     # aliases init and create
     command :init, :create do |opts| ...

     alias_command :zoo, 'foo' , '-f'
     alias_command :bar, 'baz' , 'ruby.txt'

    3. cmd =  opt_parse()

    That's all !