commit
eade2bb233
28
cmd/cmd.go
28
cmd/cmd.go
|
@ -13,7 +13,9 @@ import (
|
||||||
|
|
||||||
// CLI for command line
|
// CLI for command line
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
Name string
|
Name string
|
||||||
|
InstallName string
|
||||||
|
UninstallName string
|
||||||
|
|
||||||
install bool
|
install bool
|
||||||
uninstall bool
|
uninstall bool
|
||||||
|
@ -29,12 +31,6 @@ const (
|
||||||
// this is used when the complete is not completing words, but to
|
// this is used when the complete is not completing words, but to
|
||||||
// install it or uninstall it.
|
// install it or uninstall it.
|
||||||
func (f *CLI) Run() bool {
|
func (f *CLI) Run() bool {
|
||||||
|
|
||||||
// add flags and parse them in case they were not added and parsed
|
|
||||||
// by the main program
|
|
||||||
f.AddFlags(nil, "", "")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
err := f.validate()
|
err := f.validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Stderr.WriteString(err.Error() + "\n")
|
os.Stderr.WriteString(err.Error() + "\n")
|
||||||
|
@ -86,24 +82,24 @@ func (f *CLI) prompt() {
|
||||||
// If flags is nil, the default command line flags will be taken.
|
// If flags is nil, the default command line flags will be taken.
|
||||||
// Pass non-empty strings as installName and uninstallName to override the default
|
// Pass non-empty strings as installName and uninstallName to override the default
|
||||||
// flag names.
|
// flag names.
|
||||||
func (f *CLI) AddFlags(flags *flag.FlagSet, installName, uninstallName string) {
|
func (f *CLI) AddFlags(flags *flag.FlagSet) {
|
||||||
if flags == nil {
|
if flags == nil {
|
||||||
flags = flag.CommandLine
|
flags = flag.CommandLine
|
||||||
}
|
}
|
||||||
|
|
||||||
if installName == "" {
|
if f.InstallName == "" {
|
||||||
installName = defaultInstallName
|
f.InstallName = defaultInstallName
|
||||||
}
|
}
|
||||||
if uninstallName == "" {
|
if f.UninstallName == "" {
|
||||||
uninstallName = defaultUninstallName
|
f.UninstallName = defaultUninstallName
|
||||||
}
|
}
|
||||||
|
|
||||||
if flags.Lookup(installName) == nil {
|
if flags.Lookup(f.InstallName) == nil {
|
||||||
flags.BoolVar(&f.install, installName, false,
|
flags.BoolVar(&f.install, f.InstallName, false,
|
||||||
fmt.Sprintf("Install completion for %s command", f.Name))
|
fmt.Sprintf("Install completion for %s command", f.Name))
|
||||||
}
|
}
|
||||||
if flags.Lookup(uninstallName) == nil {
|
if flags.Lookup(f.UninstallName) == nil {
|
||||||
flags.BoolVar(&f.uninstall, uninstallName, false,
|
flags.BoolVar(&f.uninstall, f.UninstallName, false,
|
||||||
fmt.Sprintf("Uninstall completion for %s command", f.Name))
|
fmt.Sprintf("Uninstall completion for %s command", f.Name))
|
||||||
}
|
}
|
||||||
if flags.Lookup("y") == nil {
|
if flags.Lookup("y") == nil {
|
||||||
|
|
|
@ -28,7 +28,9 @@ func main() {
|
||||||
// it is possible to set custom flags name
|
// it is possible to set custom flags name
|
||||||
// so when one will type 'self -h', he will see '-complete' to install the
|
// so when one will type 'self -h', he will see '-complete' to install the
|
||||||
// completion and -uncomplete to uninstall it.
|
// 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
|
// parse the flags - both the program's flags and the completion flags
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
Loading…
Reference in New Issue