Skip to content
This repository

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

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 TODO
Octocat-spinner-32 optparse-subcommand.gemspec
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.