Class: Swamp::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli, name) ⇒ Command

Returns a new instance of Command.



11
12
13
14
15
16
# File 'lib/swamp/command.rb', line 11

def initialize(cli, name)
  @cli = cli
  @name = name
  @options = {}
  @args = []
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



8
9
10
# File 'lib/swamp/command.rb', line 8

def action
  @action
end

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/swamp/command.rb', line 9

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/swamp/command.rb', line 7

def options
  @options
end

Instance Method Details

#add_option(command, *args, &block) ⇒ Object



22
23
24
# File 'lib/swamp/command.rb', line 22

def add_option(command, *args, &block)
  @options[command] = {spec: [command] + args, block: block}
end

#parse_options(argv) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/swamp/command.rb', line 26

def parse_options(argv)
  options = {}
  OptionParser.new do |opts|
    opts.version = @cli.version
    opts.banner = "Usage: #{@cli.app_name} #{@name} [options]"
    @options.values.each do |option|
      opts_block = proc { |value| option[:block].call(value, options) }
      opts.on(*option[:spec], &opts_block)
    end
  end.parse!(argv)
  options
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
  raise Swamp::Error, e.message
end

#set_action(&block) ⇒ Object



18
19
20
# File 'lib/swamp/command.rb', line 18

def set_action(&block)
  @action = block
end