Skip to content

Commit dcc2bf3

Browse files
authored
Merge pull request #737 from univerio/switch-value-leading-hyphen
Allow leading hyphen in switch values when specified with =
2 parents 87d73c1 + 7d1bd79 commit dcc2bf3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/thor/parser/options.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false, disabl
4545
@switches = {}
4646
@extra = []
4747
@stopped_parsing_after_extra_index = nil
48+
@is_treated_as_value = false
4849

4950
options.each do |option|
5051
@switches[option.switch_name] = option
@@ -74,8 +75,19 @@ def peek
7475
end
7576
end
7677

78+
def shift
79+
@is_treated_as_value = false
80+
super
81+
end
82+
83+
def unshift(arg, is_value: false)
84+
@is_treated_as_value = is_value
85+
super(arg)
86+
end
87+
7788
def parse(args) # rubocop:disable MethodLength
7889
@pile = args.dup
90+
@is_treated_as_value = false
7991
@parsing_options = true
8092

8193
while peek
@@ -88,7 +100,10 @@ def parse(args) # rubocop:disable MethodLength
88100
when SHORT_SQ_RE
89101
unshift($1.split("").map { |f| "-#{f}" })
90102
next
91-
when EQ_RE, SHORT_NUM
103+
when EQ_RE
104+
unshift($2, is_value: true)
105+
switch = $1
106+
when SHORT_NUM
92107
unshift($2)
93108
switch = $1
94109
when LONG_RE, SHORT_RE
@@ -148,6 +163,7 @@ def assign_result!(option, result)
148163
# Two booleans are returned. The first is true if the current value
149164
# starts with a hyphen; the second is true if it is a registered switch.
150165
def current_is_switch?
166+
return [false, false] if @is_treated_as_value
151167
case peek
152168
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM
153169
[true, switch?($1)]
@@ -159,6 +175,7 @@ def current_is_switch?
159175
end
160176

161177
def current_is_switch_formatted?
178+
return false if @is_treated_as_value
162179
case peek
163180
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM, SHORT_SQ_RE
164181
true
@@ -168,6 +185,7 @@ def current_is_switch_formatted?
168185
end
169186

170187
def current_is_value?
188+
return true if @is_treated_as_value
171189
peek && (!parsing_options? || super)
172190
end
173191

spec/parser/options_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ def remaining
263263
expect(parse("-f=12")["foo"]).to eq("12")
264264
expect(parse("--foo=12")["foo"]).to eq("12")
265265
expect(parse("--foo=bar=baz")["foo"]).to eq("bar=baz")
266+
expect(parse("--foo=-bar")["foo"]).to eq("-bar")
267+
expect(parse("--foo=-bar -baz")["foo"]).to eq("-bar -baz")
266268
end
267269

268270
it "must accept underscores switch=value assignment" do
@@ -398,6 +400,7 @@ def remaining
398400

399401
it "accepts a switch=<value> assignment" do
400402
expect(parse("--attributes=name:string", "age:integer")["attributes"]).to eq("name" => "string", "age" => "integer")
403+
expect(parse("--attributes=-name:string", "age:integer", "--gender:string")["attributes"]).to eq("-name" => "string", "age" => "integer")
401404
end
402405

403406
it "accepts a switch <value> assignment" do
@@ -425,6 +428,7 @@ def remaining
425428

426429
it "accepts a switch=<value> assignment" do
427430
expect(parse("--attributes=a", "b", "c")["attributes"]).to eq(%w(a b c))
431+
expect(parse("--attributes=-a", "b", "-c")["attributes"]).to eq(%w(-a b))
428432
end
429433

430434
it "accepts a switch <value> assignment" do

0 commit comments

Comments
 (0)