diff --git a/features/todo.feature b/features/todo.feature index 2c8ade7a..b364796f 100644 --- a/features/todo.feature +++ b/features/todo.feature @@ -166,6 +166,17 @@ Feature: The todo app has a nice user interface | list --help | + Scenario: Getting Help for a top level command of todo with no command options + When I successfully run `todo help chained` + Then the output should contain: + """ + NAME + chained - + + SYNOPSIS + todo [global options] chained + """ + Scenario: Getting Help with no wrapping Given the todo app is coded to avoid wrapping text When I successfully run `todo help list` @@ -270,9 +281,9 @@ Feature: The todo app has a nice user interface create - Create a new task or context SYNOPSIS - todo [global options] create [command options] - todo [global options] create [command options] contexts [context_name] - todo [global options] create [command options] tasks task_name[, task_name]* + todo [global options] create + todo [global options] create contexts [context_name] + todo [global options] create tasks task_name[, task_name]* COMMANDS - Makes a new task diff --git a/lib/gli/commands/help_modules/command_help_format.rb b/lib/gli/commands/help_modules/command_help_format.rb index 1845641a..3b7446ef 100644 --- a/lib/gli/commands/help_modules/command_help_format.rb +++ b/lib/gli/commands/help_modules/command_help_format.rb @@ -81,8 +81,10 @@ def command_with_subcommand_usage(sub,is_default_command) def basic_usage usage = @basic_invocation.dup - usage << " [global options] #{path_to_command} " - usage << "[command options] " unless global_flags_and_switches.empty? + usage << " [global options]" unless global_flags_and_switches.empty? + usage << " #{path_to_command}" + usage << " [command options]" unless command_flags_and_switches.empty? + usage << " " usage end @@ -100,6 +102,10 @@ def global_flags_and_switches @app.flags.merge(@app.switches) end + def command_flags_and_switches + (@command.topmost_ancestor.flags_declaration_order + @command.topmost_ancestor.switches_declaration_order).select { |option| option.associated_command == @command } + end + def format_subcommands(command) commands_array = @sorter.call(command.commands_declaration_order).map { |cmd| if command.get_default_command == cmd.name diff --git a/test/tc_help.rb b/test/tc_help.rb index fe25a31d..33a1478d 100644 --- a/test/tc_help.rb +++ b/test/tc_help.rb @@ -151,6 +151,95 @@ class TestApp } end + test_that "invoking help with a known command when no global options are present omits [global options] from the usage string" do + Given a_GLI_app(:no_options) + And { + @command_name = cm = any_command_name + @desc = d = any_desc + @long_desc = ld = any_desc + @switch = s = any_option + @switch_desc = sd = any_desc + @flag = f = any_option + @flag_desc = fd = any_desc + + @app.instance_eval do + desc d + long_desc ld + command cm do |c| + + c.desc sd + c.switch s + + c.desc fd + c.flag f + + c.action {} + end + end + @command = GLI::Commands::Help.new(@app,@output,@error) + } + When { + @command.execute({},{},[@command_name]) + } + Then { + refute_output_contained(/\[global options\]/) + assert_output_contained(/\[command options\]/) + assert_output_contained('COMMAND OPTIONS') + } + end + + test_that "invoking help with a known command when no global options nor command options are present omits [global options] and [command options] from the usage string" do + Given a_GLI_app(:no_options) + And { + @command_name = cm = any_command_name + @desc = d = any_desc + @long_desc = ld = any_desc + + @app.instance_eval do + desc d + long_desc ld + command cm do |c| + c.action {} + end + end + @command = GLI::Commands::Help.new(@app,@output,@error) + } + When { + @command.execute({},{},[@command_name]) + } + Then { + refute_output_contained(/\[global options\]/) + refute_output_contained(/\[command options\]/) + refute_output_contained('COMMAND OPTIONS') + } + end + + test_that "invoking help with a known command that has no command options omits [command options] from the usage string" do + Given a_GLI_app + And { + @command_name = cm = any_command_name + @desc = d = any_desc + @long_desc = ld = any_desc + + @app.instance_eval do + desc d + long_desc ld + command cm do |c| + c.action {} + end + end + @command = GLI::Commands::Help.new(@app,@output,@error) + } + When { + @command.execute({},{},[@command_name]) + } + Then { + assert_output_contained(/\[global options\]/) + refute_output_contained(/\[command options\]/) + refute_output_contained('COMMAND OPTIONS') + } + end + test_that "omitting default_description doesn't blow up" do Given { app = TestApp.new