Skip to content

Commit

Permalink
Allow leading hyphen in switch values when specified with =
Browse files Browse the repository at this point in the history
  • Loading branch information
jzhou-stripe committed Sep 2, 2020
1 parent 34df888 commit 1e04cf3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/thor/parser/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false, disabl
@switches = {}
@extra = []
@stopped_parsing_after_extra_index = nil
@force_current_to_be_value = false

options.each do |option|
@switches[option.switch_name] = option
Expand Down Expand Up @@ -74,8 +75,19 @@ def peek
end
end

def shift
@force_current_to_be_value = false
super
end

def unshift(arg, force_value: false)
@force_current_to_be_value = force_value
super(arg)
end

def parse(args) # rubocop:disable MethodLength
@pile = args.dup
@force_current_to_be_value = false
@parsing_options = true

while peek
Expand All @@ -88,7 +100,10 @@ def parse(args) # rubocop:disable MethodLength
when SHORT_SQ_RE
unshift($1.split("").map { |f| "-#{f}" })
next
when EQ_RE, SHORT_NUM
when EQ_RE
unshift($2, force_value: true)
switch = $1
when SHORT_NUM
unshift($2)
switch = $1
when LONG_RE, SHORT_RE
Expand Down Expand Up @@ -148,6 +163,7 @@ def assign_result!(option, result)
# Two booleans are returned. The first is true if the current value
# starts with a hyphen; the second is true if it is a registered switch.
def current_is_switch?
return [false, false] if @force_current_to_be_value
case peek
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM
[true, switch?($1)]
Expand All @@ -159,6 +175,7 @@ def current_is_switch?
end

def current_is_switch_formatted?
return false if @force_current_to_be_value
case peek
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM, SHORT_SQ_RE
true
Expand All @@ -168,6 +185,7 @@ def current_is_switch_formatted?
end

def current_is_value?
return true if @force_current_to_be_value
peek && (!parsing_options? || super)
end

Expand Down
4 changes: 4 additions & 0 deletions spec/parser/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ def remaining
expect(parse("-f=12")["foo"]).to eq("12")
expect(parse("--foo=12")["foo"]).to eq("12")
expect(parse("--foo=bar=baz")["foo"]).to eq("bar=baz")
expect(parse("--foo=-bar")["foo"]).to eq("-bar")
expect(parse("--foo=-bar -baz")["foo"]).to eq("-bar -baz")
end

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

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

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

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

it "accepts a switch <value> assignment" do
Expand Down

0 comments on commit 1e04cf3

Please sign in to comment.