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

Cannot pass arguments that begin with a - #311

Open
TomWright opened this issue Apr 8, 2021 · 3 comments
Open

Cannot pass arguments that begin with a - #311

TomWright opened this issue Apr 8, 2021 · 3 comments

Comments

@TomWright
Copy link

I've found that you cannot pass arguments starting with a - to any cobra application.

I've tracked it down to this section of code:

pflag/flag.go

Lines 962 to 965 in 85dd5c8

if len(name) == 0 || name[0] == '-' || name[0] == '=' {
err = f.failf("bad flag syntax: %s", s)
return
}

The example use-cases are here:

Are there any options that can allow my application to just ignore unknown shorthand flag inputs and treat it as an argument rather than throw this error?

@cornfeedhobo
Copy link

@TomWright This is an interesting case. I'm busy today, but might find time to look this weekend. If you'd like to help me, writing a failing test would be great. Thanks.

@TomWright
Copy link
Author

The following test shows this in action:

func TestParseAllowsArgumentsStartingWithDash(t *testing.T) {
	f := NewFlagSet("parseallowsargumentsstartingwithdash", ContinueOnError)

	args := []string{"100", "-5"}
	if err := f.Parse(args); err != nil {
		t.Errorf("f.Parse() = false after Parse: %s", err)
	}
}

Imagine a calculator application that accepts two integers and will add them together.

./calc add x y

We support adding negative numbers:

./calc add 100 -5

I'm not sure what the solution should be here:

  • Ignore unknown flag inputs?
  • Escape dashes?

@TomWright
Copy link
Author

Hey @cornfeedhobo,

Sorry to chase but this is causing issues with my project. Did you get a chance to take a look at this?

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

No branches or pull requests

2 participants