Skip to content

Commit 83092ca

Browse files
authored
feat: add flag completions (#395)
1 parent d68f777 commit 83092ca

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

cmd/create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func init() {
5252
createCmd.Flags().BoolP("advanced", "a", false, "Get prompts for advanced features")
5353
createCmd.Flags().Var(&advancedFeatures, "feature", fmt.Sprintf("Advanced feature to use. Allowed values: %s", strings.Join(flags.AllowedAdvancedFeatures, ", ")))
5454
createCmd.Flags().VarP(&flagGit, "git", "g", fmt.Sprintf("Git to use. Allowed values: %s", strings.Join(flags.AllowedGitsOptions, ", ")))
55+
56+
utils.RegisterStaticCompletions(createCmd, "framework", flags.AllowedProjectTypes)
57+
utils.RegisterStaticCompletions(createCmd, "driver", flags.AllowedDBDrivers)
58+
utils.RegisterStaticCompletions(createCmd, "feature", flags.AllowedAdvancedFeatures)
59+
utils.RegisterStaticCompletions(createCmd, "git", flags.AllowedGitsOptions)
5560
}
5661

5762
type Options struct {

cmd/utils/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package utils
55
import (
66
"bytes"
77
"fmt"
8+
"log"
89
"os/exec"
910
"regexp"
1011
"strings"
1112

13+
"github.com/spf13/cobra"
1214
"github.com/spf13/pflag"
1315
)
1416

@@ -47,6 +49,16 @@ func NonInteractiveCommand(use string, flagSet *pflag.FlagSet) string {
4749
return nonInteractiveCommand
4850
}
4951

52+
func RegisterStaticCompletions(cmd *cobra.Command, flag string, options []string) {
53+
err := cmd.RegisterFlagCompletionFunc(flag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
54+
return options, cobra.ShellCompDirectiveNoFileComp
55+
})
56+
57+
if err != nil {
58+
log.Printf("warning: could not register completion for --%s: %v", flag, err)
59+
}
60+
}
61+
5062
// ExecuteCmd provides a shorthand way to run a shell command
5163
func ExecuteCmd(name string, args []string, dir string) error {
5264
command := exec.Command(name, args...)

0 commit comments

Comments
 (0)