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

Allow leading hyphen in switch values when specified with = #737

Merged
merged 3 commits into from
Jun 8, 2021

Conversation

univerio
Copy link
Contributor

@univerio univerio commented Sep 2, 2020

This is a fixed version of #498.

This allows --foo=-bar to work.

The implementation works by keeping track of an additional piece of state @force_current_to_be_value, which is maintained inside shift and unshift to ensure it matches the state in @pile.

@univerio univerio force-pushed the switch-value-leading-hyphen branch from 1e04cf3 to 214acdb Compare September 2, 2020 00:45
@@ -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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems confusing... if "-a" and "-c" are valid values for an array attribute, then why would both "-a b" and "-a b -c" give you two values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative is more confusing IMO and breaks backwards compatibility, i.e. --attributes=a b -c parsing to {"attributes" => ['a', 'b', '-c']} means that you can no longer have any options at all after an array option, which is not what the user expects.

This is the same reason this PR is limiting its scope to require a = to trigger this "leading - is a valid value" behavior.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - I think we'd need to document this specifically then.

There seems to be another PR to fix the same issue? #498 What's the difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this aims to fix the same issue as #498, but #498 appears to have been abandoned. I believe it also has a bug as described by this comment by @rafaelfranca:

I think setting this state here will broke it if there are more options after the first with - in the value, can you add tests to make sure it will work?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - let's add some documentation for this in the option/options portion then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what you're referring to by "option/options portion"? Is this something I should be adding as part of this PR?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad - I forgot what project I was talking about 😛 The update would be to the Wiki once this is merged.

lib/thor/parser/options.rb Outdated Show resolved Hide resolved
@@ -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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - let's add some documentation for this in the option/options portion then.

@@ -45,6 +45,7 @@ def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false, disabl
@switches = {}
@extra = []
@stopped_parsing_after_extra_index = nil
@is_switch_value = false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually incorrect, no? This is a value with a dash that is specifically not a switch.

Copy link
Contributor Author

@univerio univerio Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand. This instance variable is true if and only if the current element in the pile being processed is considered to be a value even if it would normally be considered an option (i.e. it has a leading -). The initial state is that nothing has been parsed yet, in which case we want to parse it as an option if it has a leading -, not as a value.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, exactly. So when this variable is true, the current argument is a value, not a switch. The name "is switch value" seems to say "the current value is actually a switch".

Maybe something like is_literal_value or something similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I landed on is_treated_as_value. PTAL @dorner

Copy link

@dorner dorner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better - looks good to me! @rafaelfranca ?

@rafaelfranca rafaelfranca merged commit dcc2bf3 into rails:master Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants