Skip to content

Commit ef1a323

Browse files
committed
Ensure dash appears in USAGE for short form alias
Although specifying single letter aliases without a dash works, it generated incorrect USAGE documentation (the dash is missing). This moves the alias normalization to happen earlier, which ensures the aliases are described correctly in the USAGE documentation.
1 parent bf0fca0 commit ef1a323

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

lib/thor/parser/option.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(name, options = {})
1111
super
1212
@lazy_default = options[:lazy_default]
1313
@group = options[:group].to_s.capitalize if options[:group]
14-
@aliases = Array(options[:aliases])
14+
@aliases = normalize_aliases(options[:aliases])
1515
@hide = options[:hide]
1616
end
1717

@@ -155,5 +155,11 @@ def undasherize(str)
155155
def dasherize(str)
156156
(str.length > 1 ? "--" : "-") + str.tr("_", "-")
157157
end
158+
159+
private
160+
161+
def normalize_aliases(aliases)
162+
Array(aliases).map { |short| short.to_s.sub(/^(?!\-)/, "-") }
163+
end
158164
end
159165
end

lib/thor/parser/options.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false, disabl
5050
options.each do |option|
5151
@switches[option.switch_name] = option
5252

53-
option.aliases.each do |short|
54-
name = short.to_s.sub(/^(?!\-)/, "-")
53+
option.aliases.each do |name|
5554
@shorts[name] ||= option.switch_name
5655
end
5756
end

spec/parser/option_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ def option(name, options = {})
108108
expect(parse([:foo, :b, "--bar"], true).name).to eq("foo")
109109
end
110110

111-
it "sets all other items as aliases" do
112-
expect(parse([:foo, :b, "--bar"], true).aliases).to eq([:b, "--bar"])
111+
it "sets all other items as normalized aliases" do
112+
expect(parse([:foo, :b, "--bar"], true).aliases).to eq(["-b", "--bar"])
113113
end
114114
end
115115
end
@@ -259,6 +259,10 @@ def option(name, options = {})
259259
it "does not negate the aliases" do
260260
expect(parse([:foo, "-f", "-b"], :boolean).usage).to eq("-f, -b, [--foo], [--no-foo]")
261261
end
262+
263+
it "normalizes the aliases" do
264+
expect(parse([:foo, :f, "-b"], :required).usage).to eq("-f, -b, --foo=FOO")
265+
end
262266
end
263267
end
264268
end

0 commit comments

Comments
 (0)