Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove special handling of inverse boolean flags #475

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions lib/thor/parser/arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ def remaining # rubocop:disable TrivialAccessors

private

def no_or_skip?(arg)
arg =~ /^--(no|skip)-([-\w]+)$/
$2
end

def last?
@pile.empty?
end
Expand Down Expand Up @@ -143,21 +138,16 @@ def parse_numeric(name)

# Parse string:
# for --string-arg, just return the current value in the pile
# for --no-string-arg, nil
# Check if the peek is included in enum if enum is provided. Otherwise raises an error.
#
def parse_string(name)
if no_or_skip?(name)
nil
else
value = shift
if @switches.is_a?(Hash) && switch = @switches[name] # rubocop:disable AssignmentInCondition
if switch.enum && !switch.enum.include?(value)
fail MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
end
value = shift
if @switches.is_a?(Hash) && switch = @switches[name] # rubocop:disable AssignmentInCondition
if switch.enum && !switch.enum.include?(value)
fail MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
end
value
end
value
end

# Raises an error if @non_assigned_required array is not empty.
Expand Down
4 changes: 0 additions & 4 deletions lib/thor/parser/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ def usage(padding = 0)

sample = "[#{sample}]" unless required?

if boolean?
sample << ", [#{dasherize("no-" + human_name)}]" unless name == "force" or name.start_with?("no-")
end

if aliases.empty?
(" " * padding) << sample
else
Expand Down
12 changes: 3 additions & 9 deletions lib/thor/parser/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,7 @@ def switch?(arg)
end

def switch_option(arg)
if match = no_or_skip?(arg) # rubocop:disable AssignmentInCondition
@switches[arg] || @switches["--#{match}"]
else
@switches[arg]
end
@switches[arg]
end

# Check if the given argument is actually a shortcut.
Expand All @@ -175,7 +171,7 @@ def parsing_options?
@parsing_options
end

# Parse boolean values which can be given as --foo=true, --foo or --no-foo.
# Parse boolean values which can be given as --foo=true or --foo.
#
def parse_boolean(switch)
if current_is_value?
Expand All @@ -189,7 +185,7 @@ def parse_boolean(switch)
true
end
else
@switches.key?(switch) || !no_or_skip?(switch)
@switches.key?(switch)
end
end

Expand All @@ -199,8 +195,6 @@ def parse_peek(switch, option)
if parsing_options? && (current_is_switch_formatted? || last?)
if option.boolean?
# No problem for boolean types
elsif no_or_skip?(switch)
return nil # User set value to nil
elsif option.string? && !option.required?
# Return the default if there is one, else the human name
return option.lazy_default || option.default || option.human_name
Expand Down
8 changes: 0 additions & 8 deletions spec/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@
expect(@content).to match(/1\n2\n3\n4\n5\n/)
end

it "does not invoke if the option is nil" do
expect(capture(:stdout) { G.start(%w[--skip-invoked]) }).not_to match(/invoke/)
end

it "prints a message if invocation cannot be found" do
content = capture(:stdout) { G.start(%w[--invoked unknown]) }
expect(content).to match(/error unknown \[not found\]/)
Expand Down Expand Up @@ -161,10 +157,6 @@
expect(@content).to match(/1\n2\n3\n4\n5\n/)
end

it "does not invoke if the option is false" do
expect(capture(:stdout) { H.start(%w[--no-defined]) }).not_to match(/invoke/)
end

it "shows invocation information to the user" do
expect(@content).to match(/invoke defined/)
end
Expand Down
22 changes: 1 addition & 21 deletions spec/parser/option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,23 +178,7 @@ def option(name, options = {})
end

it "returns usage for boolean types" do
expect(parse(:foo, :boolean).usage).to eq("[--foo], [--no-foo]")
end

it "does not use padding when no aliases are given" do
expect(parse(:foo, :boolean).usage).to eq("[--foo], [--no-foo]")
end

it "documents a negative option when boolean" do
expect(parse(:foo, :boolean).usage).to include("[--no-foo]")
end

it "does not document a negative option for a negative boolean" do
expect(parse(:'no-foo', :boolean).usage).not_to include("[--no-no-foo]")
end

it "documents a negative option for a positive boolean starting with 'no'" do
expect(parse(:'nougat', :boolean).usage).to include("[--no-nougat]")
expect(parse(:foo, :boolean).usage).to eq("[--foo]")
end

it "uses banner when supplied" do
Expand All @@ -215,10 +199,6 @@ def option(name, options = {})
it "does not show the usage between brackets" do
expect(parse([:foo, "-f", "-b"], :required).usage).to eq("-f, -b, --foo=FOO")
end

it "does not negate the aliases" do
expect(parse([:foo, "-f", "-b"], :boolean).usage).to eq("-f, -b, [--foo], [--no-foo]")
end
end
end
end
8 changes: 0 additions & 8 deletions spec/parser/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def remaining
it "accepts underscores in commandline args hash for boolean" do
create :foo_bar => :boolean
expect(parse("--foo_bar")["foo_bar"]).to eq(true)
expect(parse("--no_foo_bar")["foo_bar"]).to eq(false)
end

it "accepts underscores in commandline args hash for strings" do
Expand Down Expand Up @@ -264,11 +263,6 @@ def remaining
expect(parse("--foo_bar=http://example.com/under_score/")["foo_bar"]).to eq("http://example.com/under_score/")
end

it "accepts a --no-switch format" do
create "--foo" => "bar"
expect(parse("--no-foo")["foo"]).to be nil
end

it "does not consume an argument for --no-switch format" do
create "--cheese" => :string
expect(parse("burger", "--no-cheese", "fries")["cheese"]).to be nil
Expand Down Expand Up @@ -337,8 +331,6 @@ def remaining
it "accepts inputs in the human name format" do
create :foo_bar => :boolean
expect(parse("--foo-bar")["foo_bar"]).to eq(true)
expect(parse("--no-foo-bar")["foo_bar"]).to eq(false)
expect(parse("--skip-foo-bar")["foo_bar"]).to eq(false)
end

it "doesn't eat the next part of the param" do
Expand Down