Cobra: Add the possiblity to limit the value of a flag to a certain set

Created on 24 Dec 2019  路  9Comments  路  Source: spf13/cobra

Is possible to force the value of a option to a certain set of values?

kinquestion

Most helpful comment

I've opened an issue in pflag for those interested.

All 9 comments

@amreo,
Please be more descriptive in your issue. It is best to provide a use case and maybe some examples so you can get your point across clearly. This helps ensure there is little room for your question to be misinterpreted.

I think that this feature is provided by ValidArgs, but the implementation is currently incomplete. In #841, it is generalized so that it works with any validator.

A example may be ls with the color option. The color option allow only 'always', 'never' and 'auto' values.

package main

import (
  "fmt"
  "github.com/spf13/cobra"
)

func main() {
  var cmd = &cobra.Command{
    Short: "hello",
    Args: cobra.OnlyValidArgs,
    ValidArgs: []string{"always", "never", "auto"},
    Run: func(cmd *cobra.Command, args []string) {
      fmt.Println("Hello, World!")
    },
  }
  cmd.Execute()
}
root@a57c859ce2cf:~# go run main.go anarg
Error: invalid argument "anarg" for ""
Usage:
   [flags]

Flags:
  -h, --help   help for this command

root@a57c859ce2cf:~# go run main.go      
Hello, World!
root@a57c859ce2cf:~# go run main.go auto 
Hello, World!

That's not what I meant. I'm talking about the flag/options, and not about the args.

go run main.go --color pippo

should print "invalid value for option color"

go run main.go --color auto

should print "hello world"

You should take the snippet above as a reference, and combine it with "An example of setting the custom validator" in https://github.com/spf13/cobra#positional-and-custom-arguments and function validateArgs in #841.

Precisely, validateArgs is an example of how to check that a value is in a slice. validateArgs could actually be extended so that it could be used for finer grained tests (checking flags and not only args). Unfortunately, #841 has been on hold for almost 10 months, so I'm refraining from adding further enhancements.

Nonetheless, you might want to address this in either pflag or viper, since cobra depends on those tools to parse the flags/options.

I think it's best if the validation happened at the cobra level because that's where shell completion scripts and usage messages are generated. It would be excellent to have this for not just validation of flag arguments, but also for the completion and usage options.

I don't see why having this feature implemented in viper or pflag would impose any limitation to usage or completion. @lyda, can you please elaborate?

I've opened an issue in pflag for those interested.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

icholy picture icholy  路  6Comments

anuraaga picture anuraaga  路  5Comments

umbynos picture umbynos  路  5Comments

lukasmalkmus picture lukasmalkmus  路  4Comments

zevdg picture zevdg  路  5Comments