diff --git a/cmd/bug_report.go b/cmd/bug_report.go index c15396e..201346d 100644 --- a/cmd/bug_report.go +++ b/cmd/bug_report.go @@ -10,10 +10,12 @@ import ( func newBugReportCmd() *cobra.Command { return &cobra.Command{ - Use: "bug-report", - Short: "Submit a bug report at GitHub", - Long: "bug-report opens the default browser to start a bug report which will include useful system information.", - Example: " gup bug-report", + Use: "bug-report", + Short: "Submit a bug report at GitHub", + Long: "bug-report opens the default browser to start a bug report which will include useful system information.", + Example: " gup bug-report", + Args: cobra.NoArgs, + ValidArgsFunction: cobra.NoFileCompletions, Run: func(cmd *cobra.Command, args []string) { OsExit(bugReport(cmd, args, openBrowser)) }, diff --git a/cmd/check.go b/cmd/check.go index fdfe1fc..d6303c7 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -22,12 +22,17 @@ func newCheckCmd() *cobra.Command { check subcommand checks if the binary is the latest version and displays the name of the binary that needs to be updated. However, do not update`, + Args: cobra.NoArgs, + ValidArgsFunction: cobra.NoFileCompletions, Run: func(cmd *cobra.Command, args []string) { OsExit(check(cmd, args)) }, } cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use") + if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil { + panic(err) + } return cmd } diff --git a/cmd/export.go b/cmd/export.go index bfcb346..0e05749 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -23,6 +23,8 @@ exports the file to $XDG_CONFIG_HOME/.config/gup/gup.conf (e.g. $HOME/.config/gu After you have placed gup.conf in the same path hierarchy on another system, you execute import subcommand. gup start the installation according to the contents of gup.conf.`, + Args: cobra.NoArgs, + ValidArgsFunction: cobra.NoFileCompletions, Run: func(cmd *cobra.Command, args []string) { OsExit(export(cmd, args)) }, diff --git a/cmd/import.go b/cmd/import.go index eb5fcbd..c79e847 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -21,6 +21,8 @@ binaries across multiple systems. After you create gup.conf by import subcommand in another environment, you save conf-file in $XDG_CONFIG_HOME/.config/gup/gup.conf (e.g. $HOME/.config/gup/gup.conf.) Finally, you execute the export subcommand in this state.`, + Args: cobra.NoArgs, + ValidArgsFunction: cobra.NoFileCompletions, Run: func(cmd *cobra.Command, args []string) { OsExit(runImport(cmd, args)) }, @@ -29,7 +31,13 @@ Finally, you execute the export subcommand in this state.`, cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes") cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications") cmd.Flags().StringP("input", "i", config.FilePath(), "specify gup.conf file path to import") + if err := cmd.MarkFlagFilename("input", "conf"); err != nil { + panic(err) + } cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use") + if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil { + panic(err) + } return cmd } diff --git a/cmd/list.go b/cmd/list.go index eb96420..aef500a 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -12,9 +12,11 @@ import ( func newListCmd() *cobra.Command { return &cobra.Command{ - Use: "list", - Short: "List up command name with package path and version under $GOPATH/bin or $GOBIN", - Long: `List up command name with package path and version under $GOPATH/bin or $GOBIN`, + Use: "list", + Short: "List up command name with package path and version under $GOPATH/bin or $GOBIN", + Long: `List up command name with package path and version under $GOPATH/bin or $GOBIN`, + Args: cobra.NoArgs, + ValidArgsFunction: cobra.NoFileCompletions, Run: func(cmd *cobra.Command, args []string) { OsExit(list(cmd, args)) }, diff --git a/cmd/man.go b/cmd/man.go index e070b32..b6a02bd 100644 --- a/cmd/man.go +++ b/cmd/man.go @@ -16,10 +16,12 @@ import ( func newManCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "man", - Short: "Generate man-pages under /usr/share/man/man1 (need root privilege)", - Long: `Generate man-pages under /usr/share/man/man1 (need root privilege)`, - Example: " sudo gup man", + Use: "man", + Short: "Generate man-pages under /usr/share/man/man1 (need root privilege)", + Long: `Generate man-pages under /usr/share/man/man1 (need root privilege)`, + Example: " sudo gup man", + Args: cobra.NoArgs, + ValidArgsFunction: cobra.NoFileCompletions, Run: func(cmd *cobra.Command, args []string) { OsExit(man(cmd, args)) }, diff --git a/cmd/remove.go b/cmd/remove.go index 256e317..75cb82e 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -21,6 +21,7 @@ func newRemoveCmd() *cobra.Command { Long: `Remove command in $GOPATH/bin or $GOBIN. If you want to specify multiple binaries at once, separate them with space. [e.g.] gup remove a_cmd b_cmd c_cmd`, + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { OsExit(remove(cmd, args)) }, diff --git a/cmd/root.go b/cmd/root.go index 8473b6e..abd2d99 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,8 @@ package cmd import ( "os" + "runtime" + "strconv" "github.com/nao1215/gup/internal/assets" "github.com/nao1215/gup/internal/completion" @@ -55,3 +57,12 @@ func Execute() error { rootCmd := newRootCmd() return rootCmd.Execute() } + +func completeNCPUs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + n := runtime.NumCPU() + ret := make([]string, 0, n) + for i := 1; i <= n; i++ { + ret = append(ret, strconv.FormatInt(int64(i), 10)) + } + return ret, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/update.go b/cmd/update.go index 8f0c46e..a8dd2a3 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -34,9 +34,18 @@ under $GOPATH/bin and automatically updates commands to the latest version.`, cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes") cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications") cmd.Flags().StringSliceP("exclude", "e", []string{}, "specify binaries which should not be updated (delimiter: ',')") + if err := cmd.RegisterFlagCompletionFunc("exclude", cobra.NoFileCompletions); err != nil { + panic(err) + } cmd.Flags().StringSliceP("main", "m", []string{}, "specify binaries which update by @main or @master (delimiter: ',')") + if err := cmd.RegisterFlagCompletionFunc("main", cobra.NoFileCompletions); err != nil { + panic(err) + } // cmd.Flags().BoolP("main-all", "M", false, "update all binaries by @main or @master (delimiter: ',')") cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use") + if err := cmd.RegisterFlagCompletionFunc("jobs", completeNCPUs); err != nil { + panic(err) + } return cmd } diff --git a/cmd/version.go b/cmd/version.go index f44db2c..b7b40ed 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -9,8 +9,10 @@ import ( func newVersionCmd() *cobra.Command { return &cobra.Command{ - Use: "version", - Short: "Show " + cmdinfo.Name + " command version information", + Use: "version", + Short: "Show " + cmdinfo.Name + " command version information", + Args: cobra.NoArgs, + ValidArgsFunction: cobra.NoFileCompletions, Run: func(cmd *cobra.Command, args []string) { fmt.Println(cmdinfo.GetVersion()) },