Skip to content

Commit d2c5aa7

Browse files
committed
Properly pad aliases for option usage
Options can have aliases of arbitrary length, not just just a dash with a single letter. When printing the usage for options we should look at the actual formatted options instead of multiplying by the number 4 (the size of a single option: `-t, `). __Before (examples)__ ```console Usage: thor my_counter N [N] Options: -t, [--third=THREE] # The third argument # Default: 3 [--fourth=N] # The fourth argument z, [--simple=N] y, r, [--symbolic=N] ``` __After (examples)__ ```console Usage: thor my_counter N [N] Options: -t, [--third=THREE] # The third argument # Default: 3 [--fourth=N] # The fourth argument z, [--simple=N] y, r, [--symbolic=N] ```
1 parent 0e40ffa commit d2c5aa7

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

lib/thor/base.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ def print_options(shell, options, group_name = nil)
558558
return if options.empty?
559559

560560
list = []
561-
padding = options.map { |o| o.aliases.size }.max.to_i * 4
562-
561+
padding = options.map { |o| o.aliases_for_usage.size }.max.to_i
563562
options.each do |option|
564563
next if option.hide
565564
item = [option.usage(padding)]

lib/thor/parser/option.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ def usage(padding = 0)
9393
sample << ", [#{dasherize('no-' + human_name)}]" unless (name == "force") || name.start_with?("no-")
9494
end
9595

96+
aliases_for_usage.ljust(padding) + sample
97+
end
98+
99+
def aliases_for_usage
96100
if aliases.empty?
97-
(" " * padding) << sample
101+
""
98102
else
99-
"#{aliases.join(', ')}, #{sample}"
103+
"#{aliases.join(', ')}, "
100104
end
101105
end
102106

spec/base_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ def hello
139139
end
140140

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

146147
it "allows extra options to be given" do

0 commit comments

Comments
 (0)