cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Option to disallow use of -f|--flag <space> arg

Open jfigge opened this issue 1 year ago • 1 comments

I support the current implementation of NoOptDefVal, but what I don't like is the change in behaviour for a single field when NoOptDefVal is enabled. I.e. myApp -a aVal -b bVal -c=cVal -ddVal. The user would never know / remember to switch to c=cVal for one specific value. Therefore is it possible (without defining NoOptDefVal on all fields) to force the use of x=y on all flags in order to set a common behavior?

Looking at the code I see:

	} else if len(a) > 0 {
		// '--flag arg'
		value = a[0]
		a = a[1:]

This could be changed to something like

	} else if cobra.EnableNextArgFlagAssociation && len(a) > 0  {
		// '--flag arg' if permitted
		value = a[0]
		a = a[1:]

Originally posted by @jfigge in https://github.com/spf13/cobra/issues/866#issuecomment-1600836470

jfigge avatar Jun 21 '23 13:06 jfigge

I come across this problem,too.

For example, when I select and copy a value from terminal or webpage, and paste as the parameter of the cobra-based commandline tool, like mytool --arg <value>, the value has a leading space (use mouse cursor to select sometimes select extra spaces), which may should be removed by default.

We expect the leading space will be removed, but not. This behavior may mislead users.

hitzhangjie avatar Oct 31 '23 09:10 hitzhangjie