From f3e51b009b0229ed74509f66e083ac79c6cff865 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Tue, 26 Mar 2013 12:04:59 -0400 Subject: [PATCH] Bug 924594 - Any combination of help should never trigger wizard behavior --- lib/rhc/command_runner.rb | 57 ++++++++++++++++++++++----------------- spec/rhc/cli_spec.rb | 5 ++++ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/lib/rhc/command_runner.rb b/lib/rhc/command_runner.rb index 444341ec4..0c917b098 100644 --- a/lib/rhc/command_runner.rb +++ b/lib/rhc/command_runner.rb @@ -29,6 +29,16 @@ def options_parse_version end end + HELP_OPTIONS = ['--help', '-h'] + + def options_parse_help + if (@args & HELP_OPTIONS).present? + args = (@args -= HELP_OPTIONS) + args.shift if args.first == 'help' && !command_exists?(args.join(' ')) + exit run_help(args, nil) + end + end + # override so we can do our own error handling def run! trace = false @@ -36,15 +46,8 @@ def run! #trap('INT') { abort program(:int_message) } if program(:int_message) #trap('INT') { program(:int_block).call } if program(:int_block) - global_option('-h', '--help', 'Help on any command', :hide => true) do - # we need to remove '--h', '--he', '--hel' as well in order to avoid - # infinite recursion. - # See https://bugzilla.redhat.com/show_bug.cgi?id=920028#c3 for a detailed explanation - args = @args - %w[-h --h --he --hel --help] - command(:help).run(*args) - return - end - global_option('--version', 'Display version information', :hide => true) { say version; return } + global_option('-h', '--help', 'Help on any command', :hide => true) + global_option('--version', 'Display version information', :hide => true) # remove these because we monkey patch Commands to process all options # at once, avoiding conflicts between the global and command options @@ -59,6 +62,9 @@ def run! # special case --version so it is processed before an invalid command options_parse_version + # help is a special branch prior to command execution + options_parse_help + unless trace begin run_active_command @@ -113,7 +119,6 @@ def global_option(*args, &block) def create_default_commands command 'help options' do |c| -# c.syntax = '' c.description = "Display all global options and information about configuration" c.when_called do |args, options| say help_formatter.render_options self @@ -122,21 +127,25 @@ def create_default_commands command :help do |c| c.syntax = '' c.description = 'Display global or help documentation.' - c.when_called do |args, options| - cmd = (1..args.length).reverse_each.map{ |n| args[0,n].join(' ') }.find{ |cmd| command_exists?(cmd) } + c.when_called(&method(:run_help)) + end + end - if args.empty? - say help_formatter.render - elsif cmd.nil? - 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 - else - command = command(cmd) - help_bindings = CommandHelpBindings.new command, commands, self - say help_formatter.render_command help_bindings - end - end + def run_help(args, options) + cmd = (1..args.length).reverse_each.map{ |n| args[0,n].join(' ') }.find{ |cmd| command_exists?(cmd) } + + if args.empty? + say help_formatter.render + 0 + elsif cmd.nil? + 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." + 1 + else + command = command(cmd) + help_bindings = CommandHelpBindings.new command, commands, self + say help_formatter.render_command help_bindings + 0 end end end diff --git a/spec/rhc/cli_spec.rb b/spec/rhc/cli_spec.rb index ef1258b93..2d827c5b0 100644 --- a/spec/rhc/cli_spec.rb +++ b/spec/rhc/cli_spec.rb @@ -95,6 +95,11 @@ context 'with --help' do before(:each){ @arguments = ['--help'] } it_should_behave_like 'a global help page' + + context 'without a config file' do + before{ RHC::Wizard.stub(:has_configuration?).and_return(false) } + it_should_behave_like 'a global help page' + end end context 'with -h' do