Class: Bosh::Cli::CommandHandler
Instance Attribute Summary collapse
- #desc ⇒ String readonly
- #keywords ⇒ Array readonly
-
#options ⇒ Object
readonly
Returns the value of attribute options.
- #runner ⇒ Bosh::Cli::Runner
- #usage ⇒ String readonly
Instance Method Summary collapse
- #has_options? ⇒ Boolean
-
#initialize(klass, method, usage, desc, options = []) ⇒ CommandHandler
constructor
A new instance of CommandHandler.
- #options_summary ⇒ Object
- #parse_options(args) ⇒ Object
-
#run(args, extra_options = {}) ⇒ Integer
Run handler with provided args.
- #usage_with_params ⇒ Object
Constructor Details
#initialize(klass, method, usage, desc, options = []) ⇒ CommandHandler
Returns a new instance of CommandHandler.
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, = []) @klass = klass @method = method @usage = usage @desc = desc @options = @hints = [] @keywords = [] @parser = OptionParser.new @runner = nil extract_keywords end |
Instance Attribute Details
#keywords ⇒ Array (readonly)
5 6 7 |
# File 'lib/cli/command_handler.rb', line 5 def keywords @keywords end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/cli/command_handler.rb', line 16 def @options end |
#runner ⇒ Bosh::Cli::Runner
14 15 16 |
# File 'lib/cli/command_handler.rb', line 14 def runner @runner end |
Instance Method Details
#has_options? ⇒ Boolean
83 84 85 |
# File 'lib/cli/command_handler.rb', line 83 def @options.size > 0 end |
#options_summary ⇒ Object
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 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
108 109 110 |
# File 'lib/cli/command_handler.rb', line 108 def (args) @parser.parse!(args) end |
#run(args, extra_options = {}) ⇒ Integer
Run handler with provided args
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, = {}) 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 .each_pair do |name, value| command.add_option(format_option_name(name), value) end args = (args) begin command.send(@method.name, *args) [command.exit_code, command.info] rescue ArgumentError => ex err("#{ex.}.\n\nUsage: #{usage_with_params}") end end |
#usage_with_params ⇒ Object
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 |