You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Return an error if the same flag is provided twice with different values #72
I feel like that is a custom flag type. I don't know of any unix command that complains when the same flag is given twice. All of them I know of either use both values or the last value, depending on the type of flag...
Reacted by Albert, Kai Davenport, John Schnake and Adam S Levy
Now that pflag supports StringArray and friends I'm now using those in rclone so this issue is less important for me personally now.
However duplicated flags especially with arguments, when the parser can't cope with them, should make an error. Just because getopt(3) is lazy doesn't mean we have to be!
I feel like that is a custom flag type. I don't know of any unix command that complains when the same flag is given twice. All of them I know of either use both values or the last value, depending on the type of flag...
here's an example (not a core unix command, but it's on most systems these days)
Here's my use case: depending on the context of a sub-command, some flags might have different meaning.
In one case --foo can be given only once, while in other cases --foo might be given multiple times.
It might be confusing for a user if it's allowed to specify multiple --foo but only the last value is used.
I agree that it's reasonable to argue that it might be a bad design, but in some cases reworking an application to add a new flag --foos or converting all --foo to array and checking the length manually and failing if len(foo) != 1 might not be acceptable either.
The other use case is that the users might copy/paste or reuse commands from history and modify some of the arguments. It might happen that the command looks like --foo --bar ..... --zoo=A and the user wants to replace --bar with --bin --zoo=B. The user might be surprised by the result.
I'm happy to help designing this feature and working on PRs.
It seems like it's a reasonably small change, it's possible to reuse existing Changed field
Activity
Show error if flag is provided multiple times
eparis commentedon Mar 26, 2017
I feel like that is a custom flag type. I don't know of any unix command that complains when the same flag is given twice. All of them I know of either use both values or the last value, depending on the type of flag...
ncw commentedon Mar 27, 2017
Now that pflag supports StringArray and friends I'm now using those in rclone so this issue is less important for me personally now.
However duplicated flags especially with arguments, when the parser can't cope with them, should make an error. Just because getopt(3) is lazy doesn't mean we have to be!
dixudx commentedon Nov 13, 2017
I think we should apply this as default for all non-sliced types.
PTAL #151. I introduced a new method
AllowingMultipleSet()toFlag. For all sliced/array types, this function is enforced as default.normanr commentedon May 10, 2020
here's an example (not a core unix command, but it's on most systems these days)
dmilinevskyi commentedon May 31, 2024
I upvote this feature request.
Here's my use case: depending on the context of a sub-command, some flags might have different meaning.
In one case
--foocan be given only once, while in other cases--foomight be given multiple times.It might be confusing for a user if it's allowed to specify multiple
--foobut only the last value is used.I agree that it's reasonable to argue that it might be a bad design, but in some cases reworking an application to add a new flag
--foosor converting all--footo array and checking the length manually and failing iflen(foo) != 1might not be acceptable either.The other use case is that the users might copy/paste or reuse commands from history and modify some of the arguments. It might happen that the command looks like
--foo --bar ..... --zoo=Aand the user wants to replace--barwith--bin --zoo=B. The user might be surprised by the result.I'm happy to help designing this feature and working on PRs.
It seems like it's a reasonably small change, it's possible to reuse existing
Changedfieldpflag/flag.go
Line 481 in d5e0c06