Skip to content

Commit 091f827

Browse files
Amend previous --skip-foo with trailing option fix
The fix in bdb43eb unintentioanlly dropped support for specifying things like `--skip-foo=f` or `--skip-foo true`. In the first case, for no good reason, in the second, because "true" is now interpreted as a command option, not as a flag. This commit restores support for those, so that arguments right after `--skip-foo` flags will never be interpreted as the flag's value, unless they are "true", "t", "false", or "f". So `my_program --skip-foo true` means "run my_program without arguments and the 'foo' flag unset". whereas `my_program --skip-foo asdf` means "run my_program with argument 'asdf' and the 'foo' flag unset"
1 parent 9103e70 commit 091f827

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

lib/thor/parser/options.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,18 @@ def parsing_options?
196196
# Parse boolean values which can be given as --foo=true, --foo or --no-foo.
197197
#
198198
def parse_boolean(switch)
199-
no_or_skip = no_or_skip?(switch)
200-
if no_or_skip
201-
@switches.key?(switch) || !no_or_skip
202-
elsif current_is_value?
199+
if current_is_value?
203200
if ["true", "TRUE", "t", "T", true].include?(peek)
204201
shift
205202
true
206203
elsif ["false", "FALSE", "f", "F", false].include?(peek)
207204
shift
208205
false
209206
else
210-
!no_or_skip
207+
@switches.key?(switch) || !no_or_skip?(switch)
211208
end
212209
else
213-
@switches.key?(switch) || !no_or_skip
210+
@switches.key?(switch) || !no_or_skip?(switch)
214211
end
215212
end
216213

spec/parser/options_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ def remaining
356356
expect(parse("--skip-foo", "asdf")["skip-foo"]).to eq(true)
357357
end
358358

359+
it "will prefer 'skip-opt' variant over inverting 'opt' if explicitly set, and given a value" do
360+
create "--skip-foo" => true
361+
expect(parse("--skip-foo=f")["skip-foo"]).to eq(false)
362+
expect(parse("--skip-foo=false")["skip-foo"]).to eq(false)
363+
expect(parse("--skip-foo=t")["skip-foo"]).to eq(true)
364+
expect(parse("--skip-foo=true")["skip-foo"]).to eq(true)
365+
end
366+
359367
it "accepts inputs in the human name format" do
360368
create :foo_bar => :boolean
361369
expect(parse("--foo-bar")["foo_bar"]).to eq(true)

0 commit comments

Comments
 (0)