How to add same flag for different command?
I've tried LocalNonPersistentFlags, LocalFlags, PersistentFlags.
Flags with the same name or shorthand do not work.
Viper fails to get the value.
https://github.com/spf13/cobra#local-flag-on-parent-commands may be what you're looking for, but I'm not sure how it works with viper
Hum I don't get it: why do I need to traverse children? I would have though my command have no children.
Command "abc" has flags x and y and command "def" has flags with same name. What is the parent child relationship here?
Can you please provide an example code of your problem?
// authorizeCmd represents the authorize command
var authorizeCmd = &cobra.Command{
Use: Authorize,
Short: "Sends an authorization request to the central system.",
Long: `Before the owner of an electric vehicle can start or stop charging, the Charge Point has to authorize the
operation. The Charge Point SHALL only supply energy after authorization.`,
RunE: AuthorizeRun,
TraverseChildren: true,
}
func init() {
RootCmd.AddCommand(authorizeCmd)
authorizeCmd.Flags().StringP("auth", "a", "", "The RFID tag ID")
viper.BindPFlag("auth", authorizeCmd.Flags().Lookup("auth"))
}
This is one command with the flag 'auth'.
It works fine, until I add another command with same flag. Then it is just simply not able to read the value.
I'm wondering about the same thing too. The only way I can fix this is to add the flag to the parent command as a persistent flag.. but then i can't do different help messages for different subcommands.
Same issue trying to compose commands. Suppose you have commands b and c, and command a executes b and c. a needs to expose the a flags and b flags. This is not easy to do without cut and paste.
Same issue. Please address.
I ran into the same problem.
@lirao, @dturanski, @mcolburn, @pplcc can you please provide a MWE that reproduces your issue? None of the descriptions in this issue is detailed enough to reproduce.
Use DisableFlagParsing and parse flags in the sub command.
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
func main() {
rootCmd := &cobra.Command{
Use: "cobra",
Short: "Cobra commander",
}
fooCmd := &cobra.Command{
Use: "foo",
Short: "Print foo message",
Run: runFoo,
DisableFlagParsing: true,
}
barCmd := &cobra.Command{
Use: "bar",
Short: "Print bar message",
Run: runBar,
DisableFlagParsing: true,
}
rootCmd.AddCommand(fooCmd)
rootCmd.AddCommand(barCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runFoo(cmd *cobra.Command, args []string) {
cmd.Flags().String("print-string", "Default foo string", "Default foo string to print.")
cmd.Flags().Parse(args)
str, _ := cmd.Flags().GetString("print-string")
fmt.Printf("Foo: %s\n", str)
}
func runBar(cmd *cobra.Command, args []string) {
cmd.Flags().String("print-string", "Default bar string", "Default bar string to print.")
cmd.Flags().Parse(args)
str, _ := cmd.Flags().GetString("print-string")
fmt.Printf("Bar: %s\n", str)
}
This issue is being marked as stale due to a long period of inactivity
Most helpful comment
Same issue trying to compose commands. Suppose you have commands b and c, and command a executes b and c. a needs to expose the a flags and b flags. This is not easy to do without cut and paste.