Skip to content

Commit 5a6bd00

Browse files
authored
Merge pull request #800 from sambostock/fix-dashless-option-usage-info
Fix dashless option usage info
2 parents d54898b + ef1a323 commit 5a6bd00

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
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

@@ -159,5 +159,11 @@ def undasherize(str)
159159
def dasherize(str)
160160
(str.length > 1 ? "--" : "-") + str.tr("_", "-")
161161
end
162+
163+
private
164+
165+
def normalize_aliases(aliases)
166+
Array(aliases).map { |short| short.to_s.sub(/^(?!\-)/, "-") }
167+
end
162168
end
163169
end

lib/thor/parser/options.rb

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

55-
option.aliases.each do |short|
56-
name = short.to_s.sub(/^(?!\-)/, "-")
55+
option.aliases.each do |name|
5756
@shorts[name] ||= option.switch_name
5857
end
5958
end

spec/parser/option_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ def option(name, options = {})
105105

106106
describe "with key as an array" do
107107
it "sets the first items in the array to the name" do
108-
expect(parse([:foo, :bar, :baz], true).name).to eq("foo")
108+
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, :bar, :baz], true).aliases).to eq([:bar, :baz])
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
@@ -263,6 +263,10 @@ def option(name, options = {})
263263
it "does not negate the aliases" do
264264
expect(parse([:foo, "-f", "-b"], :boolean).usage).to eq("-f, -b, [--foo], [--no-foo]")
265265
end
266+
267+
it "normalizes the aliases" do
268+
expect(parse([:foo, :f, "-b"], :required).usage).to eq("-f, -b, --foo=FOO")
269+
end
266270
end
267271
end
268272
end

0 commit comments

Comments
 (0)