Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synopsis fix #147

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions features/todo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
<default> - Makes a new task
Expand Down
10 changes: 8 additions & 2 deletions lib/gli/commands/help_modules/command_help_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
89 changes: 89 additions & 0 deletions test/tc_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down