forked from mirror/cobra
Allow marking persistent flags as required or filename
This commit is contained in:
parent
dab963a797
commit
7290a944e4
|
@ -396,6 +396,11 @@ func (cmd *Command) MarkFlagRequired(name string) error {
|
||||||
return MarkFlagRequired(cmd.Flags(), name)
|
return MarkFlagRequired(cmd.Flags(), name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag, if it exists.
|
||||||
|
func (cmd *Command) MarkPersistentFlagRequired(name string) error {
|
||||||
|
return MarkFlagRequired(cmd.PersistentFlags(), name)
|
||||||
|
}
|
||||||
|
|
||||||
// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag in the flag set, if it exists.
|
// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag in the flag set, if it exists.
|
||||||
func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
|
func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
|
||||||
return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
|
return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
|
||||||
|
@ -407,6 +412,12 @@ func (cmd *Command) MarkFlagFilename(name string, extensions ...string) error {
|
||||||
return MarkFlagFilename(cmd.Flags(), name, extensions...)
|
return MarkFlagFilename(cmd.Flags(), name, extensions...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarkPersistentFlagFilename adds the BashCompFilenameExt annotation to the named persistent flag, if it exists.
|
||||||
|
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
|
||||||
|
func (cmd *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
|
||||||
|
return MarkFlagFilename(cmd.PersistentFlags(), name, extensions...)
|
||||||
|
}
|
||||||
|
|
||||||
// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists.
|
// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists.
|
||||||
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
|
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
|
||||||
func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
|
func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
|
||||||
|
|
|
@ -51,6 +51,12 @@ func TestBashCompletions(t *testing.T) {
|
||||||
c.Flags().StringVar(&flagval, "filename", "", "Enter a filename")
|
c.Flags().StringVar(&flagval, "filename", "", "Enter a filename")
|
||||||
c.MarkFlagFilename("filename", "json", "yaml", "yml")
|
c.MarkFlagFilename("filename", "json", "yaml", "yml")
|
||||||
|
|
||||||
|
// persistent filename
|
||||||
|
var flagvalPersistent string
|
||||||
|
c.PersistentFlags().StringVar(&flagvalPersistent, "persistent-filename", "", "Enter a filename")
|
||||||
|
c.MarkPersistentFlagFilename("persistent-filename")
|
||||||
|
c.MarkPersistentFlagRequired("persistent-filename")
|
||||||
|
|
||||||
// filename extensions
|
// filename extensions
|
||||||
var flagvalExt string
|
var flagvalExt string
|
||||||
c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")
|
c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")
|
||||||
|
@ -72,6 +78,7 @@ func TestBashCompletions(t *testing.T) {
|
||||||
|
|
||||||
// check for required flags
|
// check for required flags
|
||||||
check(t, str, `must_have_one_flag+=("--introot=")`)
|
check(t, str, `must_have_one_flag+=("--introot=")`)
|
||||||
|
check(t, str, `must_have_one_flag+=("--persistent-filename=")`)
|
||||||
// check for custom completion function
|
// check for custom completion function
|
||||||
check(t, str, `COMPREPLY=( "hello" )`)
|
check(t, str, `COMPREPLY=( "hello" )`)
|
||||||
// check for required nouns
|
// check for required nouns
|
||||||
|
|
Loading…
Reference in New Issue