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:



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

def desc
  @desc
end

#keywordsArray (readonly)

Returns:

  • (Array)


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

def keywords
  @keywords
end

#runnerBosh::Cli::Runner

Returns:



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

def runner
  @runner
end

#usageString (readonly)

Returns:



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

def usage
  @usage
end

Instance Method Details

#has_options?Boolean

Returns:

  • (Boolean)


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

def has_options?
  @options.size > 0
end

#options_summaryObject



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

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).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



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

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
63
# File 'lib/cli/command_handler.rb', line 41

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

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

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

  args = parse_options(args)

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

#usage_with_paramsObject



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

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