No commit activity in last 3 years
No release in over 3 years
Add subcommand parsing to Ruby's OptionParser class
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 12.0
= 2.9.0
 Project Readme

optparse-subcommand

Build Status

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.