spf13 / pflag

Drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
BSD 3-Clause "New" or "Revised" License
2.43k stars 348 forks source link

Support TryAddFlagSet and EnvVars and AddFlag #333

Open robberphex opened 3 years ago

robberphex commented 3 years ago
  1. Add func TryAddFlag{,Set}, which add flag{Set} and ignore conflicts.
  2. Support EnvVars, and show env name in help message.
  3. Support AddFlag. originally, AddFlag can accept a pflag.Flag, but user cannot build Value field for pflag.Flag. So I rename pflag.newStringValue to pflag.NewStringValue to make AddFlag usable, for example:
    child.Flags().AddFlag(
        &pflag.Flag{
            Name:     "testArg",
            Usage:    "usage",
            EnvVars:  []string{"TEST_ARG", "TEST_ARG1"},
            Value:    pflag.NewStringValue("default", new(string)),
        },
    )
fahedouch commented 3 years ago

@robberphex

It looks like it is not showing correctly the default value

-l, --leaderElection        Use leader election for the conroller manager. (default ) [$LEADER_ELECTION]

Extract from my source code

    leaderElection := new(bool)
    rootCmd.PersistentFlags().AddFlag(&pflag.Flag{
        Name:      "leaderElection",
        Shorthand: "l",
        Usage:     "Use leader election for the conroller manager.",
        EnvVars:   []string{"LEADER_ELECTION"},
        Value:     pflag.NewBoolValue(false, leaderElection),
    }
AkihiroSuda commented 3 years ago

Could you update the commit messages to be more descriptive?

fahedouch commented 2 years ago

@robberphex do you plan to move forward on this?

AkihiroSuda commented 2 years ago

nerdctl now uses unforked cobra, so this PR is no longer needed from nerdctl perspective. https://github.com/containerd/nerdctl/pull/524

fahedouch commented 2 years ago

thanks @AkihiroSuda