Skip to content
Myr

Allow marking persistent flags as required or filename

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Fabiano Franz · committed by spf13 · parent dab963a · Browse files · 2 files+18 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−0 removed

  • other:+18 added−0 removed

2 files+18 added−0 removed

modified:bash_completions_test.go+7 added
49 unmodified lines
50 var flagval string51 c.Flags().StringVar(&flagval, "filename", "", "Enter a filename")52 c.MarkFlagFilename("filename", "json", "yaml", "yml")5354 // persistent filename55 var flagvalPersistent string56 c.PersistentFlags().StringVar(&flagvalPersistent, "persistent-filename", "", "Enter a filename")57 c.MarkPersistentFlagFilename("persistent-filename")58 c.MarkPersistentFlagRequired("persistent-filename")5960 // filename extensions61 var flagvalExt string62 c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")63 c.MarkFlagFilename("filename-ext")
13 unmodified lines
77 check(t, str, "_cobra-test_print")7879 // check for required flags80 check(t, str, `must_have_one_flag+=("--introot=")`)81 check(t, str, `must_have_one_flag+=("--persistent-filename=")`)82 // check for custom completion function83 check(t, str, `COMPREPLY=( "hello" )`)84 // check for required nouns85 check(t, str, `must_have_one_noun+=("pods")`)
9 unmodified lines
modified:bash_completions.go+11 added
394 unmodified lines
395func (cmd *Command) MarkFlagRequired(name string) error {396 return MarkFlagRequired(cmd.Flags(), name)397}398399// MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag, if it exists.400func (cmd *Command) MarkPersistentFlagRequired(name string) error {401 return MarkFlagRequired(cmd.PersistentFlags(), name)402}403404// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag in the flag set, if it exists.405func MarkFlagRequired(flags *pflag.FlagSet, name string) error {406 return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})407}
3 unmodified lines
411func (cmd *Command) MarkFlagFilename(name string, extensions ...string) error {412 return MarkFlagFilename(cmd.Flags(), name, extensions...)413}414415// MarkPersistentFlagFilename adds the BashCompFilenameExt annotation to the named persistent flag, if it exists.416// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.417func (cmd *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {418 return MarkFlagFilename(cmd.PersistentFlags(), name, extensions...)419}420421// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists.422// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.423func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {424 return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
1 unmodified line