From 7290a944e4fe564fead7dbb3935f3ce495eb5685 Mon Sep 17 00:00:00 2001 From: Fabiano Franz Date: Wed, 4 Nov 2015 14:42:43 -0500 Subject: [PATCH] Allow marking persistent flags as required or filename --- bash_completions.go | 11 +++++++++++ bash_completions_test.go | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/bash_completions.go b/bash_completions.go index 113bb311..6259f8c2 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -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 { diff --git a/bash_completions_test.go b/bash_completions_test.go index a1f2b13c..53656c7f 100644 --- a/bash_completions_test.go +++ b/bash_completions_test.go @@ -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