1 // Package cmd used for command line options for the complete tool
11 "github.com/posener/complete/cmd/install"
14 // CLI for command line
26 defaultInstallName = "install"
27 defaultUninstallName = "uninstall"
30 // Run is used when running complete in command line mode.
31 // this is used when the complete is not completing words, but to
32 // install it or uninstall it.
33 func (f *CLI) Run() bool {
36 os.Stderr.WriteString(err.Error() + "\n")
43 err = install.Install(f.Name)
46 err = install.Uninstall(f.Name)
48 // non of the action flags matched,
49 // returning false should make the real program execute
54 fmt.Printf("%s failed! %s\n", f.action(), err)
61 // prompt use for approval
62 // exit if approval was not given
63 func (f *CLI) prompt() {
64 defer fmt.Println(f.action() + "ing...")
68 fmt.Printf("%s completion for %s? ", f.action(), f.Name)
72 switch strings.ToLower(answer) {
76 fmt.Println("Cancelling...")
81 // AddFlags adds the CLI flags to the flag set.
82 // If flags is nil, the default command line flags will be taken.
83 // Pass non-empty strings as installName and uninstallName to override the default
85 func (f *CLI) AddFlags(flags *flag.FlagSet) {
87 flags = flag.CommandLine
90 if f.InstallName == "" {
91 f.InstallName = defaultInstallName
93 if f.UninstallName == "" {
94 f.UninstallName = defaultUninstallName
97 if flags.Lookup(f.InstallName) == nil {
98 flags.BoolVar(&f.install, f.InstallName, false,
99 fmt.Sprintf("Install completion for %s command", f.Name))
101 if flags.Lookup(f.UninstallName) == nil {
102 flags.BoolVar(&f.uninstall, f.UninstallName, false,
103 fmt.Sprintf("Uninstall completion for %s command", f.Name))
105 if flags.Lookup("y") == nil {
106 flags.BoolVar(&f.yes, "y", false, "Don't prompt user for typing 'yes'")
111 func (f *CLI) validate() error {
112 if f.install && f.uninstall {
113 return errors.New("Install and uninstall are mutually exclusive")
118 // action name according to the CLI values.
119 func (f *CLI) action() string {