Cobra: Ignore flags after position or Positional catch-all

Created on 3 Oct 2019  Â·  5Comments  Â·  Source: spf13/cobra

Is there a way to define the following behaviour for flags and positional arguments:
mycommand [flags] positioned-arg positioned-arg-catch-all

For example:
mycommand -v -f text.out ping -c5 8.8.8.8

Desired Output

Flag[0]: -v
Flag[1]: -f
Arg[0]: text.out
Arg[1]: ping -c5 8.8.8.8

Actual Output

Flag[0]: -v
Flag[1]: -f
Flag[1]: -c5
Arg[0]: text.out
Arg[1]: ping
Arg[2]: 8.8.8.8

kinquestion

Most helpful comment

I wanted to keep it without the quotes

You can use strings.Join(args[1:], " ") in the (sub)commands where you want this behaviour. I think this is a very specific use case to have it merged into the codebase.

Disabling the flag validation I can get it to work, but then I have to parse each input manually.

This might be a legit bug. Do you mean that -c5 is recognized as an invalid cobra/pflag argument and it produces a failure? If this is the case, I would suggest to enhance your first comment by adding minimal working examples that help us reproduce the issue. Possible examples to try:

mycommand -v -f text.out "ping -c5 8.8.8.8"
mycommand -v -f text.out ping -c5 8.8.8.8
mycommand -v ping -c5 8.8.8.8
mycommand -v "ping -c5 8.8.8.8"
mycommand -v -c5 8.8.8.8
mycommand -v "-c5 8.8.8.8"

mycommand -v -f text.out -- ping -c5 8.8.8.8
mycommand -f text.out -v -- ping -c5 8.8.8.8
mycommand -v -- ping -c5 8.8.8.8
mycommand -v -- -c5 8.8.8.8

All 5 comments

What about mycommand -v -f text.out "ping -c5 8.8.8.8"?

@umarcor I wanted to keep it without the quotes, as my app will be calling another app and I would like to keep the same syntax.

Disabling the flag validation I can get it to work, but then I have to parse each input manually.

But if there is no other way, that should suffice. Thank you. :+1:

I wanted to keep it without the quotes

You can use strings.Join(args[1:], " ") in the (sub)commands where you want this behaviour. I think this is a very specific use case to have it merged into the codebase.

Disabling the flag validation I can get it to work, but then I have to parse each input manually.

This might be a legit bug. Do you mean that -c5 is recognized as an invalid cobra/pflag argument and it produces a failure? If this is the case, I would suggest to enhance your first comment by adding minimal working examples that help us reproduce the issue. Possible examples to try:

mycommand -v -f text.out "ping -c5 8.8.8.8"
mycommand -v -f text.out ping -c5 8.8.8.8
mycommand -v ping -c5 8.8.8.8
mycommand -v "ping -c5 8.8.8.8"
mycommand -v -c5 8.8.8.8
mycommand -v "-c5 8.8.8.8"

mycommand -v -f text.out -- ping -c5 8.8.8.8
mycommand -f text.out -v -- ping -c5 8.8.8.8
mycommand -v -- ping -c5 8.8.8.8
mycommand -v -- -c5 8.8.8.8

@umarcor using the double quotes or the double dash works. -c5 only gets recognised as an invalid flag when I don't use them. So I will be closing this issue. Thank you for your help.

for me using the -- like in kubectl exec command separates the positional args from the cobra flags

for example:

➜ go run main.go  cmd --flag --  bin/bash -c 'echo $API_KEY && echo going to sleep && sleep 5

Was this page helpful?
0 / 5 - 0 ratings

Related issues

icholy picture icholy  Â·  6Comments

mdaymard picture mdaymard  Â·  5Comments

jjzcru picture jjzcru  Â·  3Comments

joernott picture joernott  Â·  6Comments

alapidas picture alapidas  Â·  3Comments