# File lib/rhc/command_runner.rb, line 70 def create_default_commands command :help do |c| c.syntax = 'rhc help <command>' c.description = 'Display global or <command> help documentation.' c.when_called do |args, options| if args.empty? say help_formatter.render else command = command args.join(' ') begin require_valid_command command rescue InvalidCommandError => e 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." next end help_bindings = CommandHelpBindings.new command, commands, Commander::Runner.instance.options say help_formatter.render_command help_bindings end end end end
# File lib/rhc/command_runner.rb, line 63 def global_option(*args, &block) opts = args.pop if Hash === args.last super(*args, &block).tap do |options| options.last.merge!(opts) if opts end end
# File lib/rhc/command_runner.rb, line 59 def provided_arguments @args[0, @args.find_index { |arg| arg.start_with?('-') } || @args.length] end
override so we can do our own error handling
# File lib/rhc/command_runner.rb, line 10 def run! trace = false require_program :version, :description trap('INT') { abort program(:int_message) } if program(:int_message) trap('INT') { program(:int_block).call } if program(:int_block) global_option('-h', '--help', 'Display help documentation') do args = @args - %w[-h --help] command(:help).run(*args) return end global_option('-v', '--version', 'Display version information') { say version; return } global_option('-t', '--trace', 'Display backtrace when an error occurs') { trace = true } parse_global_options remove_global_options options, @args unless trace begin run_active_command rescue InvalidCommandError => e if provided_arguments.empty? say RHC::HelpFormatter.new(self).render else 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." end 1 rescue ArgumentError, OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument => e help_bindings = CommandHelpBindings.new(active_command, commands, Commander::Runner.instance.options) usage = RHC::HelpFormatter.new(self).render_command(help_bindings) RHC::Helpers.error e.message say "\n#{usage}" 1 rescue RHC::Exception, RHC::Rest::Exception => e RHC::Helpers.error e.message e.code.nil? ? 128 : e.code end else run_active_command end end
regex fix from git - match on word boundries
# File lib/rhc/command_runner.rb, line 4 def valid_command_names_from *args arg_string = args.delete_if { |value| value =~ %r^-/ }.join ' ' commands.keys.find_all { |name| name if %r^#{name}\b/.match arg_string } end