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

flag value misparsed as subcommand #1777

Closed
colin-arnott-xero opened this issue Aug 18, 2022 · 5 comments · Fixed by #1781
Closed

flag value misparsed as subcommand #1777

colin-arnott-xero opened this issue Aug 18, 2022 · 5 comments · Fixed by #1781
Labels
area/flags-args Changes to functionality around command line flags and args kind/bug A bug in cobra; unintended behavior

Comments

@colin-arnott-xero
Copy link

colin-arnott-xero commented Aug 18, 2022

it looks like the parser is having trouble with space delimited persistent flags, where a sub command aligns with the value of the first of many flags:

for ./app -z one -o one two: https://go.dev/play/p/cKaEuTEhWAZ
wanted:

zero: one
one: one
args: []

got:

zero: -o
one: nil
args: [one two]

note: this does not trigger for the following cases

app -z two two          # only one flag
app -z=two -o=one two   # using equals delimited flags
app -z one -o two two   # colliding value is not first flag
app two -z two -one one # sub command comes first
@brianpursley
Copy link
Contributor

Interesting.

I can confirm this with kubectl (which uses cobra). In the example below, I try to run a pod in a namespace called "run", using the run command.

Fails:

$ kubectl -n run -v 1 run bar --image=nginx
Error from server (NotFound): namespaces "-v" not found

This works:

$ kubectl run -n run -v 1 bar --image=nginx
pod/bar created

@brianpursley
Copy link
Contributor

Another thing that is weird about this:

If you have multiple sub commands, the problem only occurs when the flag value matches the particular sub command you are running.

So for example, kubectl has a create sub command, but using -n create with the run sub command works fine:

$ kubectl -n create -v 1 run bar --image=nginx
pod/bar created

But if I use -n create with the create sub command, it fails:

$ kubectl -n create -v 1 create -f ~/foo.yaml
error: Unexpected args: [1 create]
See 'kubectl create -h' for help and examples

@brianpursley
Copy link
Contributor

I think I see what's causing this and it shouldn't be too complicated to fix. I will take a look at it and submit a PR with a fix.

@github-actions
Copy link

github-actions bot commented Nov 7, 2022

The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed.
    You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interseted in reopening

@colin-arnott-xero
Copy link
Author

still broken

hoshsadiq pushed a commit to zulucmd/zulu that referenced this issue May 16, 2023
When the command searches args to find the arg matching a
particular subcommand name, it needs to ignore flag values,
as it is possible that the value for a flag might match
the name of the sub command.

This change improves argsMinusFirstX() to ignore flag values
when it searches for the X to exclude from the result.

Fixes spf13/cobra#1777

Merge spf13/cobra#1781
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/flags-args Changes to functionality around command line flags and args kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants