0

I set the following flags in a cobra command

    varsCmd.PersistentFlags().BoolP("secrets", "", false, usageMsgFlagEnvsVarsSecrets)
    varsCmd.PersistentFlags().BoolP("non-secrets", "", false, usageMsgFlagEnvsVarsNonSecrets)

Then i create a subcommand

setCmd.Flags().StringSliceP("from-literal", "l", []string{}, usageMsgFlagEnvsSetFromLiteral)
varsCmd.AddCommand(compareVarsCmd, getCmd, setCmd)

Usage of setCmd is set and of varsCmd is vars

Also, setCmd has:

Args: cobra.ExactArgs(1),

When I invoke my program

go run cli/main.go env vars set argument --from-literal --non-secrets --secrets

I see that the value of --from-literal is [--non-secrets]

I assume this is because --from-literal is a StringSlice type of flag. Is there a way to prevent this from happening in a user-friendly way?

pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • 2
    This is typical behavior for any command line program: if you have an option `--foo` that takes a string argument, there's no way for a cli parser to know that a following `--bar` is anything other than the argument for `--foo`. What if you *wanted* the value of `--from-literal` to be `--non-secrets`? You could write an option parser that only supports `--flag=value`, but the behavior would surprise people who are used to conventional option handling. – larsks Feb 12 '23 at 14:05

0 Answers0