Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Add popular subcommand-style option parsing to Ruby's stdlib OptionParser class
branch: master

Merge pull request #1 from magec/master

Show subcommands in `--help` message
latest commit 30fae70e14
Bo Jeanes authored
Failed to load latest commit information.
lib/optparse OptionParser#summarize now gets inside subcommands.
spec/optparse OptionParser#summarize now gets inside subcommands.
.gitignore Ignore gem output file
.rspec
.travis.yml Initial commit
Gemfile Initial commit
LICENSE Initial commit
README.md Add to README
Rakefile Initial commit
TODO
optparse-subcommand.gemspec Don't make parse! method private

README.md

optparse-subcommand

This gem extends Ruby's standard library OptionParser class to allow popular subcommand-style tools (e.g. git).

Usage

A new method subcommand is added to the OptionParser instance allowing you to define an action to occur when that subcommand is detected as an argument.

The subcommand method takes a block which yields a new OptionParser instance. Simply define your option parsing recursively for all subcommands.

Example:

require 'optparse/subcommand' # Also requires 'optparse'
require 'ostruct'

options                = OpenStruct.new
options.verbose        = false
options.action         = nil
options.action_options = OpenStruct.new

parser = OptionParser.new do |opts|
  opts.on '-v', '--[no-]verbose' do |verbose|
    options.verbose = verbose
  end

  opts.subcommand 'foo' do |foo|
    options.action = :foo

    foo.on '-x' do
      options.action_options.x = true
    end
  end
end

parser.parse(%w[-v foo -x]) # or ARGV, whatever...

options # => #<OpenStruct verbose=true, action=:foo, action_options=#<OpenStruct x=true>>

License

See LICENSE file in this directory.

Something went wrong with that request. Please try again.