Class: Gem::Portage::Command

Inherits:
Command
  • Object
show all
Defined in:
lib/g-gem/command.rb

Direct Known Subclasses

PortageCommand

Defined Under Namespace

Classes: Option

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, summary = nil, defaults = nil) ⇒ Command



4
5
6
7
8
9
10
11
12
13
# File 'lib/g-gem/command.rb', line 4

def initialize(command, summary=nil, defaults=nil)
  super(command, summary, defaults || {})
  options_defaults = {}
  class_command_options.each_pair do |k, option|
    add_option(*option.optparse_args, &option.handler)
    options_defaults[option.opt_id] = option.default unless option.default.nil?
    extend option.module
  end
  self.defaults = options_defaults unless defaults
end

Class Method Details

.class_command_optionsObject



50
51
52
53
54
55
56
# File 'lib/g-gem/command.rb', line 50

def class_command_options
  @command_options ||= if superclass.respond_to?(:class_command_options) 
                         superclass.class_command_options.dup
                       else
                         {}
                       end
end

.has_option(id, *getopt_args, &handler) ⇒ Object



30
31
32
33
# File 'lib/g-gem/command.rb', line 30

def has_option(id, *getopt_args, &handler)
  option =  Option.new(id, *getopt_args, &handler)
  class_command_options[option.opt_id] = option
end

.has_options_from(other, *opts) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/g-gem/command.rb', line 35

def has_options_from(other, *opts)
  defaults = Hash === opts.last ? opts.pop : {}
  borrowed = opts.empty? ? other.class_command_options.keys : opts + defaults.keys
  borrowed.each do |key|
    next unless key
    opt = other.class_command_options[key].dup
    opt.default = defaults[key] if defaults.has_key? key
    class_command_options[opt.opt_id] = opt
  end
end

.registerObject



46
47
48
# File 'lib/g-gem/command.rb', line 46

def register
  new.register
end

Instance Method Details

#class_command_options(*args, &prc) ⇒ Object



25
26
27
# File 'lib/g-gem/command.rb', line 25

def class_command_options(*args, &prc)
  self.class.class_command_options(*args, &prc)
end

#defaults_strObject



15
16
17
18
19
# File 'lib/g-gem/command.rb', line 15

def defaults_str
  defaults.keys.inject("") do |str, opt|
    str + " " + class_command_options[opt].to_s
  end
end

#registerObject



21
22
23
# File 'lib/g-gem/command.rb', line 21

def register
  command_manager.register_command self
end