3

I have a CLI with a flag name that I would like to rename, but I would also like to make it so this change is backwards compatible and using the old flag name still works, such as below:

# New flag name
root subcommand --no-color

# Old flag name still works
root subcommand --nocolour

My original thought with this was to alias the flag name to the new name as described here. This approach does change the flag name to the new name, but it does not make it so you can use the old name of a flag as well.

I have put in place an approach to solve this by defining another flag with the same properties but a different name and also moving the shorthands to the new flag, such as below:

// Old flag name
cmd.PersistentFlags().BoolP(
    "nocolour", "", false,
    "disable colouring (default: false)")

// New flag name
cmd.PersistentFlags().BoolP(
    "no-color", "C", false,
    "disable coloring (default: false)")

And then I mark the old name flag as hidden:

cmd.PersistentFlags().MarkHidden("nocolour")

I am wondering if there is a way to accomplish what I did above without having to define and support two flags?

dhelfand
  • 141
  • 1
  • 7

1 Answers1

2

I used the SetNormalizeFunc function provided by cobra to do rename a old flag value to a new one.

func addFlags() {
    // Define a new flag `ignore-var-run` and use the same variable opts.IgnoreVarRun to capture the flag value.
    RootCmd.PersistentFlags().BoolVarP(&opts.IgnoreVarRun, "ignore-var-run", "", true, "Ignore /var/run directory when taking image snapshot. Set it to false to preserve /var/run/ in destination image. (Default true).")
    
    // Keep the old flag `whitelist-var-run` as is.
    RootCmd.PersistentFlags().BoolVarP(&opts.IgnoreVarRun, "whitelist-var-run", "", true, "Ignore /var/run directory when taking image snapshot. Set it to false to preserve /var/run/ in destination image. (Default true).")
    
    // Mark the old flag as deprecated. 
    RootCmd.PersistentFlags().MarkDeprecated("whitelist-var-run", "please use ignore-var-run instead.")

    // SetNormalize function to translate the use of `whitelist-var-run` to `ignore-var-run`
    RootCmd.PersistentFlags().SetNormalizeFunc(normalizeWhitelistVarRun)
}

func normalizeWhitelistVarRun(f *pflag.FlagSet, name string) pflag.NormalizedName {
    switch name {
    case "whitelist-var-run":
        name = "ignore-var-run"
        break
    }
    return pflag.NormalizedName(name)
}

Here is the PR for this https://github.com/GoogleContainerTools/kaniko/pull/1668/

Tej
  • 356
  • 2
  • 5