Class: Decline::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/decline/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



8
9
10
11
12
13
# File 'lib/decline/command.rb', line 8

def initialize
  @config = OpenStruct.new
  @parser = OptionParser.new
  @subcommands ||= Hash.new
  self.options(@parser) # Callback to the child class
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/decline/command.rb', line 6

def config
  @config
end

#parserObject (readonly)

Returns the value of attribute parser.



6
7
8
# File 'lib/decline/command.rb', line 6

def parser
  @parser
end

#subcommandsObject (readonly)

Returns the value of attribute subcommands.



6
7
8
# File 'lib/decline/command.rb', line 6

def subcommands
  @subcommands
end

Instance Method Details

#add_command(name, command) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/decline/command.rb', line 15

def add_command(name, command)
  command.add_options do |cli|
    cli.set_program_name "#{parser.program_name} #{name}"
  end

  subcommands[name.to_s] = command

  self
end

#add_options(&block) ⇒ Object



25
26
27
28
# File 'lib/decline/command.rb', line 25

def add_options(&block)
  block.call(parser)
  self
end

#run(argv, parent_config = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/decline/command.rb', line 30

def run(argv, parent_config={})
  merge_config(parent_config)
  # I didn't want to use `order!` here because it will mutate argv, but it
  parsed_args = Array(parser.order!(argv))
  call_command(parsed_args)
end