class Commander::Command

Attributes

default_action[RW]

Public Instance Methods

default_action?() click to toggle source
# File lib/rhc/commands.rb, line 9
def default_action?
  default_action.present?
end
parse_options_and_call_procs(*args) click to toggle source
# File lib/rhc/commands.rb, line 13
def parse_options_and_call_procs *args
  runner = Commander::Runner.instance
  opts = OptionParser.new

  # add global options
  runner.options.each do |option|
    opts.on(*option[:args], &runner.global_option_proc(option[:switches], &option[:proc]))
  end

  # add command options
  @options.each do |option|
    opts.on(*option[:args], &option[:proc])
    opts
  end

  remaining = opts.parse! args

  _, config_path = proxy_options.find{ |arg| arg[0] == :config }
  clean, _ = proxy_options.find{ |arg| arg[0] == :clean }

  begin
    @config = RHC::Config.new
    @config.use_config(config_path) if config_path

    unless clean
      @config.to_options.each_pair do |key, value|
        next if proxy_options.detect{ |arr| arr[0] == key }
        if sw = opts.send(:search, :long, key.to_s.gsub(%r_/, '-'))
          _, cb, val = sw.send(:conv_arg, nil, value) {|*exc| raise(*exc) }
          cb.call(val) if cb
        else
          proxy_options << [key, value]
        end
      end
    end
  rescue ArgumentError => e
    n = OptionParser::InvalidOption.new(e.message)
    n.reason = "The configuration file #{@config.path} contains an invalid setting"
    n.set_backtrace(e.backtrace)
    raise n
  rescue OptionParser::ParseError => e
    e.reason = "The configuration file #{@config.path} contains an invalid setting"
    raise
  end
  remaining
end