From 5ba73666ab38b2d2524b42bf3dc83c1449b55b30 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 20 May 2017 22:15:37 +0300 Subject: [PATCH 1/2] cmd: fix complete flags name --- cmd/cmd.go | 27 +++++++++++++++------------ example/self/main.go | 4 +++- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index f796ec8..224279e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -13,7 +13,9 @@ import ( // CLI for command line type CLI struct { - Name string + Name string + InstallName string + UninstallName string install bool uninstall bool @@ -30,10 +32,11 @@ const ( // install it or uninstall it. func (f *CLI) Run() bool { + fs := flag.NewFlagSet(f.Name, 0) // add flags and parse them in case they were not added and parsed // by the main program - f.AddFlags(nil, "", "") - flag.Parse() + f.AddFlags(fs) + fs.Parse(os.Args[1:]) err := f.validate() if err != nil { @@ -86,24 +89,24 @@ func (f *CLI) prompt() { // If flags is nil, the default command line flags will be taken. // Pass non-empty strings as installName and uninstallName to override the default // flag names. -func (f *CLI) AddFlags(flags *flag.FlagSet, installName, uninstallName string) { +func (f *CLI) AddFlags(flags *flag.FlagSet) { if flags == nil { flags = flag.CommandLine } - if installName == "" { - installName = defaultInstallName + if f.InstallName == "" { + f.InstallName = defaultInstallName } - if uninstallName == "" { - uninstallName = defaultUninstallName + if f.UninstallName == "" { + f.UninstallName = defaultUninstallName } - if flags.Lookup(installName) == nil { - flags.BoolVar(&f.install, installName, false, + if flags.Lookup(f.InstallName) == nil { + flags.BoolVar(&f.install, f.InstallName, false, fmt.Sprintf("Install completion for %s command", f.Name)) } - if flags.Lookup(uninstallName) == nil { - flags.BoolVar(&f.uninstall, uninstallName, false, + if flags.Lookup(f.UninstallName) == nil { + flags.BoolVar(&f.uninstall, f.UninstallName, false, fmt.Sprintf("Uninstall completion for %s command", f.Name)) } if flags.Lookup("y") == nil { diff --git a/example/self/main.go b/example/self/main.go index 068a0ac..ae4c2e4 100644 --- a/example/self/main.go +++ b/example/self/main.go @@ -28,7 +28,9 @@ func main() { // it is possible to set custom flags name // so when one will type 'self -h', he will see '-complete' to install the // completion and -uncomplete to uninstall it. - cmp.AddFlags(nil, "complete", "uncomplete") + cmp.CLI.InstallName = "complete" + cmp.CLI.UninstallName = "uncomplete" + cmp.AddFlags(nil) // parse the flags - both the program's flags and the completion flags flag.Parse() From 14dcbd6b21157407d501d08d8d2ee16e1027b564 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 20 May 2017 22:29:12 +0300 Subject: [PATCH 2/2] cmd: remove adding of flags from run --- cmd/cmd.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 224279e..7137dee 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -31,13 +31,6 @@ const ( // this is used when the complete is not completing words, but to // install it or uninstall it. func (f *CLI) Run() bool { - - fs := flag.NewFlagSet(f.Name, 0) - // add flags and parse them in case they were not added and parsed - // by the main program - f.AddFlags(fs) - fs.Parse(os.Args[1:]) - err := f.validate() if err != nil { os.Stderr.WriteString(err.Error() + "\n")