Skip to content

Commit

Permalink
Merge pull request #810 from p8/fix/option-padding
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed May 10, 2023
2 parents c16e7a1 + 17619ce commit ea10821
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
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

0 comments on commit ea10821

Please sign in to comment.