Skip to content

Commit

Permalink
Properly pad aliases for option usage
Browse files Browse the repository at this point in the history
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]
```
  • Loading branch information
p8 committed Feb 9, 2023
1 parent 0e40ffa commit 17619ce
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 17619ce

Please sign in to comment.