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
Is the "cant duplicate this flag" error (v3) makes sense? #1926
I have a question that might be a bit silly, but I was confused when I encountered the error can't duplicate this flag while using only one flag with the OnlyOnce: true property for a cli.Command.
Ouch, I could provide a bit more context on how exactly I encountered this error.
Usually, if the app I develop has more than one sub-command that requires the same flags, I prefer to define every flag that I could reuse in a separate package named shared:
If you run each test one by one (not together - that's important), each of them will pass. But, if you run them in parallel (go test ./...), the test will fail with the error:
=== RUN TestCommand_RunFail
=== PAUSE TestCommand_RunFail
=== CONT TestCommand_RunFail
command_test.go:47:
Error Trace: /path/to/command_test.go:666
Error: Received unexpected error:
invalid value "4321" for flag -port: cant duplicate this flag
Test: TestCommand_RunFail
--- FAIL: TestCommand_RunFail (0.00s)
@tarampampam The OnlyOnce field is used to tell the cli library to expect this flag on the cmd line only once. So if it is specified twice , for example
--port 1234 --port 4566
then you will get that error. This is to protect users from themselves. Without this option in above example port would be set to 4566
Activity
tarampampam commentedon Jun 21, 2024
Ouch, I could provide a bit more context on how exactly I encountered this error.
Usually, if the app I develop has more than one sub-command that requires the same flags, I prefer to define every flag that I could reuse in a separate package named
shared:And use it in my commands in this manner:
Everything works fine until I write a unit test to test the command execution:
If you run each test one by one (not together - that's important), each of them will pass. But, if you run them in parallel (
go test ./...), the test will fail with the error:To fix this, all I need to do is replace
with
This was so strange that I decided to create this issue to highlight this workaround.
Note
TL;DR - flags have their own state, so don't reuse them using pointers.
dearchap commentedon Jun 27, 2024
@tarampampam The OnlyOnce field is used to tell the cli library to expect this flag on the cmd line only once. So if it is specified twice , for example
then you will get that error. This is to protect users from themselves. Without this option in above example port would be set to 4566