Class: Bosh::Cli::CommandHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/command_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, method, usage, desc, options = []) ⇒ CommandHandler

Returns a new instance of CommandHandler.

Parameters:

  • klass (Class)
  • method (UnboundMethod)
  • usage (String)
  • desc (String)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cli/command_handler.rb', line 22

def initialize(klass, method, usage, desc, options = [])
  @klass = klass
  @method = method
  @usage = usage
  @desc = desc

  @options = options

  @hints = []
  @keywords = []

  @parser = OptionParser.new
  @runner = nil
  extract_keywords
end

Instance Attribute Details

#descString (readonly)

Returns:



11
12
13
# File 'lib/cli/command_handler.rb', line 11

def desc
  @desc
end

#keywordsArray (readonly)

Returns:

  • (Array)


5
6
7
# File 'lib/cli/command_handler.rb', line 5

def keywords
  @keywords
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/cli/command_handler.rb', line 16

def options
  @options
end

#runnerBosh::Cli::Runner

Returns:



14
15
16
# File 'lib/cli/command_handler.rb', line 14

def runner
  @runner
end

#usageString (readonly)

Returns:



8
9
10
# File 'lib/cli/command_handler.rb', line 8

def usage
  @usage
end

Instance Method Details

#has_options?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/cli/command_handler.rb', line 83

def has_options?
  @options.size > 0
end

#options_summaryObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cli/command_handler.rb', line 87

def options_summary
  result = []
  padding = 5

  margin = @options.inject(0) do |max, (name, _)|
    [max, name.size].max
  end

  @options.each do |(name, desc)|
    desc = desc.select { |word| word.is_a?(String) }
    column_width = terminal_width - padding - margin

    result << name.ljust(margin).make_yellow + " " +
      desc.join(" ").columnize(
        column_width, [margin + 1, name.size + 1].max)
  end

  result.join("\n")
end

#parse_options(args) ⇒ Object

Parameters:

  • args (Array)

    Arguments to parse



108
109
110
# File 'lib/cli/command_handler.rb', line 108

def parse_options(args)
  @parser.parse!(args)
end

#run(args, extra_options = {}) ⇒ Integer

Run handler with provided args

Parameters:

  • args (Array)

Returns:

  • (Integer)

    Command exit code



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cli/command_handler.rb', line 41

def run(args, extra_options = {})
  command = @klass.new(@runner)

  @options.each do |(name, arguments)|
    @parser.on(name, *arguments) do |value|
      command.add_option(format_option_name(name), value)
    end
  end

  extra_options.each_pair do |name, value|
    command.add_option(format_option_name(name), value)
  end

  args = parse_options(args)

  begin
    command.send(@method.name, *args)
    command.exit_code
  rescue ArgumentError => _
    err("Usage: #{usage_with_params}")
  end
end

#usage_with_paramsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cli/command_handler.rb', line 64

def usage_with_params
  result = [@usage]
  @method.parameters.each do |parameter|
    next if parameter.size < 2
    kind, name = parameter
    if kind == :opt
      result << "[<#{name}>]"
    elsif kind == :req
      result << "<#{name}>"
    end
  end

  @options.each do |(name, _)|
    result << "[#{name}]"
  end

  result.join(" ")
end