mirror of
https://github.com/spf13/cobra.git
synced 2024-11-15 01:38:17 -07:00
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)
|
||||
}
|
||||
|
||||
// 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.
|
||||
func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
|
||||
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...)
|
||||
}
|
||||
|
||||
// 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.
|
||||
// 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 {
|
||||
|
@ -51,6 +51,12 @@ func TestBashCompletions(t *testing.T) {
|
||||
c.Flags().StringVar(&flagval, "filename", "", "Enter a filename")
|
||||
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
|
||||
var flagvalExt string
|
||||
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(t, str, `must_have_one_flag+=("--introot=")`)
|
||||
check(t, str, `must_have_one_flag+=("--persistent-filename=")`)
|
||||
// check for custom completion function
|
||||
check(t, str, `COMPREPLY=( "hello" )`)
|
||||
// check for required nouns
|
||||
|
Loading…
Reference in New Issue
Block a user