module OptionParser::Subcommand

Private Class Methods

included(klass) click to toggle source
# File lib/optparse/subcommand.rb, line 12
def self.included(klass)
  klass.instance_eval do
    alias_method :parse_without_subcommand!, :parse!
    alias_method :parse!, :parse_with_subcommand!
    public :parse!

    alias_method :summarize_without_subcommand, :summarize
    alias_method :summarize, :summarize_with_subcommand
  end
end

Public Instance Methods

subcommand(key, &block) click to toggle source
# File lib/optparse/subcommand.rb, line 5
def subcommand(key, &block)
  subcommand = lambda { OptionParser.new(&block) }
  subcommands[key.to_s] = subcommand
end

Private Instance Methods

parse_with_subcommand!(*args) click to toggle source
# File lib/optparse/subcommand.rb, line 23
def parse_with_subcommand!(*args)
  subcommand = catch(:subcommand) do
    return order!(*args) { |sub| throw :subcommand, sub }
  end

  sub_parser = subcommands[subcommand.to_s]
  sub_parser.call.send(:parse!, *args) if sub_parser
end
subcommands() click to toggle source
# File lib/optparse/subcommand.rb, line 42
def subcommands
  @subcommands ||= {}
end
summarize_with_subcommand(to = [], width = @summary_width, max = width - 1, indent = @summary_indent, &blk) click to toggle source
# File lib/optparse/subcommand.rb, line 32
def summarize_with_subcommand(to = [], width = @summary_width, max = width - 1, indent = @summary_indent, &blk)
  summarize_without_subcommand(to, width, max, indent, &blk)
  to << "\n"
  subcommands.each do |key, sub_parser|
    to << indent + key + "\n"
    sub_parser.call.send(:summarize,to,width,max,indent + "  ",&blk)
  end
  to
end