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

Properly pad aliases for option usage #810

Merged
merged 1 commit into from
May 10, 2023
Merged
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
3 changes: 1 addition & 2 deletions lib/thor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,7 @@ def print_options(shell, options, group_name = nil)
return if options.empty?

list = []
padding = options.map { |o| o.aliases.size }.max.to_i * 4

padding = options.map { |o| o.aliases_for_usage.size }.max.to_i
options.each do |option|
next if option.hide
item = [option.usage(padding)]
Expand Down
8 changes: 6 additions & 2 deletions lib/thor/parser/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ def usage(padding = 0)
sample << ", [#{dasherize('no-' + human_name)}]" unless (name == "force") || name.start_with?("no-")
end

aliases_for_usage.ljust(padding) + sample
end

def aliases_for_usage
if aliases.empty?
(" " * padding) << sample
""
else
"#{aliases.join(', ')}, #{sample}"
"#{aliases.join(', ')}, "
end
end

Expand Down
5 changes: 3 additions & 2 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ def hello
end

it "use padding in options that do not have aliases" do
expect(@content).to match(/^ -t, \[--third/)
expect(@content).to match(/^ \[--fourth/)
expect(@content).to match(/^ -t, \[--third/)
expect(@content).to match(/^ \[--fourth/)
expect(@content).to match(/^ y, r, \[--symbolic/)
end

it "allows extra options to be given" do
Expand Down