Skip to content

Commit 17619ce

Browse files
committed
Properly pad aliases for option usage
When printing the options of a command, options without aliases are padded so they aligned with options with aliases The size of the padding is calculated by multiplying the max number of aliases for an option by the number 4 (a dash, a letter, a comma and a space?). Options can have aliases of arbitrary length, not just just a dash with a single letter. For example in Rails the `main` option has the [alias](https://github.com/rails/rails/blob/main/railties/lib/rails/generators/app_base.rb#L100)` --master`. Also, the current implementation adds padding only to options without aliases. This results in strange output when callings `bin/rails new -h`: ```console -T, [--skip-test], [--no-skip-test] # Skip test files [--skip-system-test], [--no-skip-system-test] # Skip system test files [--skip-bootsnap], [--no-skip-bootsnap] # Skip bootsnap gem ... [--edge], [--no-edge] # Set up the application with a Gemfile pointing to the main branch on the Rails repository --master, [--main], [--no-main] # Set up the application with Gemfile pointing to Rails repository main branch ``` When printing the usage for options we should look at the actual formatted options. __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 17619ce

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)