Class: Commander::Command

Inherits:
Object show all
Defined in:
lib/rhc/commands.rb,
lib/rhc/commands.rb

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_actionObject

Returns the value of attribute default_action.



8
9
10
# File 'lib/rhc/commands.rb', line 8

def default_action
  @default_action
end

#infoObject

Returns the value of attribute info.



8
9
10
# File 'lib/rhc/commands.rb', line 8

def info
  @info
end

#rootObject

Returns the value of attribute root.



8
9
10
# File 'lib/rhc/commands.rb', line 8

def root
  @root
end

Instance Method Details

#default_action?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/rhc/commands.rb', line 9

def default_action?
  default_action.present?
end

#deprecated(as_alias = nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/rhc/commands.rb', line 49

def deprecated(as_alias=nil)
  return false unless info
  return info[:deprecated] if info[:deprecated]
  return false unless info[:aliases]
  info[:aliases].select{ |a| ['-',' '].map{ |s| Array(a[:action]).join(s) }.include?(as_alias) }.map{ |a| a[:deprecated] }.first if as_alias
end

#option(*args, &block) ⇒ Object



17
18
19
20
21
22
# File 'lib/rhc/commands.rb', line 17

def option(*args, &block)
  opts = args.pop if Hash === args.last
  option_old(*args, &block).tap do |options|
    options.last.merge!(opts) if opts
  end
end

#option_oldObject



16
# File 'lib/rhc/commands.rb', line 16

alias_method :option_old, :option

#parse_options_and_call_procs(*args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rhc/commands.rb', line 56

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

  # Separate option lists with '--'
  remaining = args.split('--').map{ |a| opts.parse!(a) }.inject([]) do |arr, h|
    arr << '--'
    arr.concat(h)
  end
  remaining.shift

  _, 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
    $terminal.debug("Using config file #{@config.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(/_/, '-'))
          _, 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

#proxy_option_structObject

Force proxy_option_struct to default to nil for values, backported for Commander 4.0.3



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rhc/commands.rb', line 28

def proxy_option_struct
  proxy_options.inject Options.new do |options, (option, value)|
    # options that are present will evaluate to true
    value = true if value.nil?
    # if multiple values were specified for this option, collect it as an
    # array. on 'fill_arguments' we will decide between stick with the array
    # (if :type => :list) or just take the last value from array.
    # not part of the backported method.
    if proxy_options.select{ |item| item[0] == option }.length > 1
      if options[option]
        options[option] << value
      else
        options.__send__ :"#{option}=", [value]
      end
    else
      options.__send__ :"#{option}=", value
    end
    options
  end
end

#root?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/rhc/commands.rb', line 12

def root?
  root.present?
end