| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "github.com/spf13/cobra" |
| 5 | ) |
| 6 | |
| 7 | func init() { |
| 8 | // File extension hints for positional args |
| 9 | |
| 10 | convertCmd.ValidArgsFunction = completeMarkdownFiles |
| 11 | embedCmd.ValidArgsFunction = completeMarkdownFiles |
| 12 | fmtCmd.ValidArgsFunction = completeXMLFiles |
| 13 | extractCmd.ValidArgsFunction = completeXMLFiles |
| 14 | verifyCmd.ValidArgsFunction = completeXMLFiles |
| 15 | pullCmd.ValidArgsFunction = cobra.NoFileCompletions |
| 16 | pushCmd.ValidArgsFunction = completePushArgs |
| 17 | |
| 18 | // Flag file completions |
| 19 | _ = convertCmd.RegisterFlagCompletionFunc("output", completeXMLFilesFlag) |
| 20 | _ = embedCmd.RegisterFlagCompletionFunc("output", completeXMLFilesFlag) |
| 21 | _ = embedCmd.RegisterFlagCompletionFunc("template", completeXMLFilesFlag) |
| 22 | _ = extractCmd.RegisterFlagCompletionFunc("output", completeMarkdownFilesFlag) |
| 23 | _ = fmtCmd.RegisterFlagCompletionFunc("output", completeXMLFilesFlag) |
| 24 | _ = pullCmd.RegisterFlagCompletionFunc("output", completeMarkdownFilesFlag) |
| 25 | |
| 26 | // Marker flag completions |
| 27 | for _, cmd := range []*cobra.Command{embedCmd, extractCmd, pushCmd} { |
| 28 | _ = cmd.RegisterFlagCompletionFunc("marker-start", cobra.NoFileCompletions) |
| 29 | _ = cmd.RegisterFlagCompletionFunc("marker-end", cobra.NoFileCompletions) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func completeMarkdownFiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 34 | return []string{"md"}, cobra.ShellCompDirectiveFilterFileExt |
| 35 | } |
| 36 | |
| 37 | func completeXMLFiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 38 | return []string{"xml"}, cobra.ShellCompDirectiveFilterFileExt |
| 39 | } |
| 40 | |
| 41 | func completePushArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 42 | if len(args) == 0 { |
| 43 | // First arg is URL — no file completion |
| 44 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 45 | } |
| 46 | // Second arg is input file |
| 47 | return []string{"md"}, cobra.ShellCompDirectiveFilterFileExt |
| 48 | } |
| 49 | |
| 50 | func completeMarkdownFilesFlag(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 51 | return []string{"md"}, cobra.ShellCompDirectiveFilterFileExt |
| 52 | } |
| 53 | |
| 54 | func completeXMLFilesFlag(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 55 | return []string{"xml"}, cobra.ShellCompDirectiveFilterFileExt |
| 56 | } |
| 57 | |