Building from What is the difference between go-Cobra PersistentFlags and Flags?:
Assume we have a top-level prog
command, and two start
and stop
subcommands in it.
Assume the prog
command has a persistent flag called database
.
How can we make this flag required in the start
command only? Note that the stop
command should have the flag but it should be optional.
progCmd.MarkPersistentFlagRequired("database")
does not work as intended, as it would make the flag required in both start
and stop
.
startCmd.MarkPersistentFlagRequired("database")
does not work either:
no such flag -database