summaryrefslogtreecommitdiffstats
path: root/commands/new.go
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2024-04-07 20:33:17 +0000
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-04-11 15:34:26 +0200
commita67650b6f764d201fde22b1bc3c06b923fdc5438 (patch)
tree698bdb49baf78317ad506fcf8642a0f6da883543 /commands/new.go
parent2a060b37a32c3831ece2203b8b5451adb3c787af (diff)
completion: Improve existing argument completions, add many more
Do not offer filenames to arguments not taking one, complete arguments of options taking resource kinds, directory names, --logLevel, release --step, config and new --format. As an internal refactoring, use higher level functions to set flag completions. SetAnnotation works, but is more verbose than alternatives, and uses bash specific wording. While at it, move setting completions next to flag definitions consistently. Remove superfluous --destination completer setting, which is already set elsewhere.
Diffstat (limited to 'commands/new.go')
-rw-r--r--commands/new.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/commands/new.go b/commands/new.go
index 79d2c9e7e..144da7cd7 100644
--- a/commands/new.go
+++ b/commands/new.go
@@ -24,6 +24,7 @@ import (
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/create"
"github.com/gohugoio/hugo/create/skeletons"
+ "github.com/gohugoio/hugo/resources/kinds"
"github.com/spf13/cobra"
)
@@ -60,8 +61,16 @@ Ensure you run this within the root directory of your site.`,
return create.NewContent(h, contentType, args[0], force)
},
withc: func(cmd *cobra.Command, r *rootCommand) {
+ cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
+ if len(args) != 0 {
+ return []string{}, cobra.ShellCompDirectiveNoFileComp
+ }
+ return []string{}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveFilterDirs
+ }
cmd.Flags().StringVarP(&contentType, "kind", "k", "", "content type to create")
+ _ = cmd.RegisterFlagCompletionFunc("kind", cobra.FixedCompletions(kinds.AllKindsInPages, cobra.ShellCompDirectiveNoFileComp))
cmd.Flags().String("editor", "", "edit new content with this editor, if provided")
+ _ = cmd.RegisterFlagCompletionFunc("editor", cobra.NoFileCompletions)
cmd.Flags().BoolVarP(&force, "force", "f", false, "overwrite file if it already exists")
applyLocalFlagsBuildConfig(cmd, r)
},
@@ -103,8 +112,15 @@ Use ` + "`hugo new [contentPath]`" + ` to create new content.`,
return nil
},
withc: func(cmd *cobra.Command, r *rootCommand) {
+ cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
+ if len(args) != 0 {
+ return []string{}, cobra.ShellCompDirectiveNoFileComp
+ }
+ return []string{}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveFilterDirs
+ }
cmd.Flags().BoolVarP(&force, "force", "f", false, "init inside non-empty directory")
cmd.Flags().StringVar(&format, "format", "toml", "preferred file format (toml, yaml or json)")
+ _ = cmd.RegisterFlagCompletionFunc("format", cobra.FixedCompletions([]string{"toml", "yaml", "json"}, cobra.ShellCompDirectiveNoFileComp))
},
},
&simpleCommand{
@@ -137,6 +153,9 @@ according to your needs.`,
return nil
},
+ withc: func(cmd *cobra.Command, r *rootCommand) {
+ cmd.ValidArgsFunction = cobra.NoFileCompletions
+ },
},
},
}