Class: RHC::CommandRunner
- Defined in:
- lib/rhc/command_runner.rb
Constant Summary collapse
- HELP_OPTIONS =
['--help', '-h']
Instance Method Summary collapse
- #create_default_commands ⇒ Object
- #global_option(*args, &block) ⇒ Object
- #options_parse_debug ⇒ Object
- #options_parse_help ⇒ Object
- #options_parse_trace ⇒ Object
- #options_parse_version ⇒ Object
-
#program(*args) ⇒ Object
:nocov:.
- #provided_arguments ⇒ Object
-
#run! ⇒ Object
override so we can do our own error handling.
- #run_help(args = [], options = nil) ⇒ Object
-
#valid_command_names_from(*args) ⇒ Object
regex fix from git - match on word boundries.
Instance Method Details
#create_default_commands ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rhc/command_runner.rb', line 116 def create_default_commands command 'help options' do |c| c.description = "Display all global options and information about configuration" c.when_called do |args, | say help_formatter. self 0 end end command :help do |c| c.syntax = '<command>' c.description = 'Display global or <command> help documentation.' c.when_called(&method(:run_help)) end end |
#global_option(*args, &block) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/rhc/command_runner.rb', line 109 def global_option(*args, &block) opts = args.pop if Hash === args.last super(*args, &block).tap do || .last.merge!(opts) if opts end end |
#options_parse_debug ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/rhc/command_runner.rb', line 25 def if @args.include?("-d") or @args.include?("--debug") @args.delete "-d" @args.delete "--debug" return true end false end |
#options_parse_help ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/rhc/command_runner.rb', line 43 def if (@args & HELP_OPTIONS).present? args = (@args -= HELP_OPTIONS) args.shift if args.first == 'help' && !command_exists?(args.join(' ')) exit run_help(args) end end |
#options_parse_trace ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/rhc/command_runner.rb', line 17 def if @args.include?("--trace") @args.delete "--trace" return true end false end |
#options_parse_version ⇒ Object
34 35 36 37 38 39 |
# File 'lib/rhc/command_runner.rb', line 34 def if @args.include? "--version" say version exit 0 end end |
#program(*args) ⇒ Object
:nocov:
11 12 13 |
# File 'lib/rhc/command_runner.rb', line 11 def program(*args) Array(super).first end |
#provided_arguments ⇒ Object
105 106 107 |
# File 'lib/rhc/command_runner.rb', line 105 def provided_arguments @args[0, @args.find_index { |arg| arg != '--' and arg.start_with?('-') } || @args.length] end |
#run! ⇒ Object
override so we can do our own error handling
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rhc/command_runner.rb', line 52 def run! trace = false require_program :version, :description global_option('-h', '--help', 'Help on any command', :hide => true) global_option('--version', 'Display version information', :hide => true) # special case --debug so all commands can output relevant info on it $terminal.debug = # special case --trace because we need to use it in the runner trace = # special case --version so it is processed before an invalid command # help is a special branch prior to command execution unless trace begin run_active_command rescue InvalidCommandError => e run_help(provided_arguments) rescue \ OptionParser::InvalidOption => e RHC::Helpers.error e. 1 rescue \ ArgumentError, OptionParser::ParseError => e help_bindings = CommandHelpBindings.new(active_command, commands, self) usage = RHC::HelpFormatter.new(self).render_command_syntax(help_bindings) = case e when OptionParser::AmbiguousOption "The option #{e.args.join(' ')} is ambiguous. You will need to specify the entire option." else e. end RHC::Helpers.error say "#{usage}" 1 rescue RHC::Exception, RHC::Rest::Exception => e RHC::Helpers.error e. e.code.nil? ? 128 : [1, (e.code || 1).to_i].max end else run_active_command end end |
#run_help(args = [], options = nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/rhc/command_runner.rb', line 131 def run_help(args=[], =nil) args.delete_if{ |a| a.start_with? '-' } unless args[0] == 'commands' variations = (1..args.length).reverse_each.map{ |n| args[0,n].join('-') } cmd = variations.find{ |cmd| command_exists?(cmd) } end if args.empty? say help_formatter.render 0 else if cmd.nil? matches = (variations || ['']).inject(nil) do |candidates, term| term = term.downcase keys = commands.keys.map(&:downcase) prefix, keys = keys.partition{ |n| n.start_with? term } inline, keys = keys.partition{ |n| n.include? term } break [term, prefix, inline] unless prefix.empty? && inline.empty? end unless matches RHC::Helpers.error "The command '#{program :name} #{provided_arguments.join(' ')}' is not recognized.\n" say "See '#{program :name} help' for a list of valid commands." return 1 end candidates = (matches[1] + matches[2]).map{ |n| commands[n] }.uniq.sort_by{ |c| c.name } if candidates.length == 1 cmd = candidates.first.name else RHC::Helpers.pager RHC::Helpers.say matches[0] != '' ? "Showing commands matching '#{matches[0]}'" : "Showing all commands" candidates.reverse.each do |command| RHC::Helpers.paragraph do aliases = (commands.map{ |(k,v)| k if command == v }.compact - [command.name]).map{ |s| "'#{s}'"} aliases[0] = "(also #{aliases[0]}" if aliases[0] aliases[-1] << ')' if aliases[0] RHC::Helpers.header [RHC::Helpers.color(command.name, :cyan), *aliases.join(', ')] say command.description || command.summary end end return 1 end end RHC::Helpers.pager command = command(cmd) help_bindings = CommandHelpBindings.new command, commands, self say help_formatter.render_command help_bindings 0 end end |
#valid_command_names_from(*args) ⇒ Object
regex fix from git - match on word boundries
4 5 6 7 |
# File 'lib/rhc/command_runner.rb', line 4 def valid_command_names_from *args arg_string = args.delete_if { |value| value =~ /^-/ }.join ' ' commands.keys.find_all { |name| name if /^#{name}\b/.match arg_string } end |