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 passing value starting with - to custom option #192

Merged
merged 1 commit into from
Dec 21, 2018

Conversation

XenoPhex
Copy link
Contributor

@XenoPhex XenoPhex commented Sep 1, 2016

We have a case where some of our custom flags for the Cloud Foundry CLI have string values that take -1 as a value to signal special cases. For example, when we're setting a Maximum Allowed Memory Size (eg 1MB, 4GB, etc...) and we want this value to be unlimited, we use -1.

This pr will enable custom flags to take values with -.

XenoPhex pushed a commit to cloudfoundry/cli that referenced this pull request Sep 1, 2016
Revert this commit once jessevdk/go-flags#192
has been merged into go-flags.

[#127131179]

Signed-off-by: Anand Gaitonde <agaitonde@pivotal.io>
@jessevdk
Copy link
Owner

jessevdk commented Sep 3, 2016

I don't like to special case this for custom options only. If this should be supported, then I think it should be supported for all options.

The reason this is not the case, is because it leads to ambiguous parsing. In particular with flags that have an optional argument, we would need a different rule.

Therefore, the decision has been that if you do need to specify a value starting with a -, you need to use the --option=-value syntax instead. An exception was made recently for negative numbers because it makes sense intuitively to allow it (the user intent is quite clear in that case).

@luan
Copy link

luan commented Sep 6, 2016

@jessevdk I agree with your last point about the user intent being clear. But I think the same argument is valid on the use case @XenoPhex describes: the user there would be passing in -1 to signify that they want no memory limits, so the user intent is clear. Another way to look at this is if your custom flag type was a number with unit, for example:

distance -x1 5in -y1 -5in -x2 -10ft -y2 1ft

Or even for strings:

run-wrapper --cmd "echo" --flags "-n" --arguments "hello world"

So the argument I'm trying to make here is that allowing - as a first character in a value for a custom flag should be allowed and the onus of making sure that user intent is clear is on the custom type itself. I see that this could be misused but I say that the value of allowing this is greater than the risk of it being misused.

@XenoPhex
Copy link
Contributor Author

any decisions on this?

@jessevdk
Copy link
Owner

Ok, your argument has convinced me. However, I would like that this works by adding a value validator for custom flags (i.e. isValidValue). The support for numbers vs not allowing - is part of the default isValidValue, but custom types can override this.

@XenoPhex
Copy link
Contributor Author

Not really closed - I rebased incorrectly, will update shortly.

@XenoPhex
Copy link
Contributor Author

XenoPhex commented Sep 25, 2018

@jessevdk Do you mean for isValidValue to be a private method (if so, on what?) or a public/interface method that any custom flag can implement?

@XenoPhex
Copy link
Contributor Author

I've updated the pull request with my best interpretation of what you said. Please let me know if it is correct.

@jessevdk
Copy link
Owner

Sorry, what I meant was indeed an interface that a custom type may implement that can do validation of the option value. I did not mean that any custom type with a custom unmarshal will accept any kind of value.

@XenoPhex
Copy link
Contributor Author

I'll resubmit this pull when I have some time later today.

@XenoPhex
Copy link
Contributor Author

XenoPhex commented Oct 2, 2018

@jessevdk the adjustment has been submitted, please let me know if you need further changes!

option.go Outdated Show resolved Hide resolved
convert.go Outdated Show resolved Hide resolved
parser.go Outdated Show resolved Hide resolved
@XenoPhex XenoPhex force-pushed the master branch 2 times, most recently from 6cb61d5 to dac4e0a Compare October 17, 2018 05:08
@XenoPhex
Copy link
Contributor Author

@jessevdk I've made the changes you've requested, but it looks like the Travis is failing on trying to get golint, which is weird.

I also can't seem to retrigger the build. So... hoping you can take a stick to it and get it working. Tests & lint passed locally.

@XenoPhex
Copy link
Contributor Author

This is blocked on #283.

@XenoPhex
Copy link
Contributor Author

@jessevdk rebased the lint changes, let me know if there's anything more I can do. Thanks!

@michaelabon
Copy link

Is this good to merge, @jessevdk?

@ewrenn8
Copy link

ewrenn8 commented Dec 10, 2018

Hi @jessevdk, we are wondering if this PR can be merged?

Thanks,
Eli

Copy link
Owner

@jessevdk jessevdk left a comment

Choose a reason for hiding this comment

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

Cool, thanks!

@jessevdk jessevdk merged commit c0795c8 into jessevdk:master Dec 21, 2018
@XenoPhex
Copy link
Contributor Author

Thanks for the merge!!

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.

5 participants