]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
completion: Improve existing argument completions, add many more
authorVille Skyttä <ville.skytta@iki.fi>
Sun, 7 Apr 2024 20:33:17 +0000 (20:33 +0000)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 11 Apr 2024 13:34:26 +0000 (15:34 +0200)
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.

12 files changed:
commands/commandeer.go
commands/config.go
commands/convert.go
commands/deploy.go
commands/env.go
commands/gen.go
commands/import.go
commands/list.go
commands/mod.go
commands/new.go
commands/release.go
commands/server.go

index a0e4e38369640122542eb980289daf36ad8e1681..f18c3f81311c05441c74347d62f55712735ba6b1 100644 (file)
@@ -48,6 +48,7 @@ import (
        "github.com/gohugoio/hugo/helpers"
        "github.com/gohugoio/hugo/hugofs"
        "github.com/gohugoio/hugo/hugolib"
+       "github.com/gohugoio/hugo/resources/kinds"
        "github.com/spf13/afero"
        "github.com/spf13/cobra"
 )
@@ -482,47 +483,47 @@ Complete documentation is available at https://gohugo.io/.`
 
        // Configure persistent flags
        cmd.PersistentFlags().StringVarP(&r.source, "source", "s", "", "filesystem path to read files relative from")
-       cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
+       _ = cmd.MarkFlagDirname("source")
        cmd.PersistentFlags().StringP("destination", "d", "", "filesystem path to write files to")
-       cmd.PersistentFlags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{})
+       _ = cmd.MarkFlagDirname("destination")
 
        cmd.PersistentFlags().StringVarP(&r.environment, "environment", "e", "", "build environment")
+       _ = cmd.RegisterFlagCompletionFunc("environment", cobra.NoFileCompletions)
        cmd.PersistentFlags().StringP("themesDir", "", "", "filesystem path to themes directory")
+       _ = cmd.MarkFlagDirname("themesDir")
        cmd.PersistentFlags().StringP("ignoreVendorPaths", "", "", "ignores any _vendor for module paths matching the given Glob pattern")
+       _ = cmd.RegisterFlagCompletionFunc("ignoreVendorPaths", cobra.NoFileCompletions)
        cmd.PersistentFlags().String("clock", "", "set the clock used by Hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00")
+       _ = cmd.RegisterFlagCompletionFunc("clock", cobra.NoFileCompletions)
 
        cmd.PersistentFlags().StringVar(&r.cfgFile, "config", "", "config file (default is hugo.yaml|json|toml)")
+       _ = cmd.MarkFlagFilename("config", config.ValidConfigFileExtensions...)
        cmd.PersistentFlags().StringVar(&r.cfgDir, "configDir", "config", "config dir")
+       _ = cmd.MarkFlagDirname("configDir")
        cmd.PersistentFlags().BoolVar(&r.quiet, "quiet", false, "build in quiet mode")
        cmd.PersistentFlags().BoolVar(&r.renderToMemory, "renderToMemory", false, "render to memory (mostly useful when running the server)")
 
-       // Set bash-completion
-       _ = cmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, config.ValidConfigFileExtensions)
-
        cmd.PersistentFlags().BoolVarP(&r.verbose, "verbose", "v", false, "verbose output")
        cmd.PersistentFlags().BoolVarP(&r.debug, "debug", "", false, "debug output")
        cmd.PersistentFlags().StringVar(&r.logLevel, "logLevel", "", "log level (debug|info|warn|error)")
+       _ = cmd.RegisterFlagCompletionFunc("logLevel", cobra.FixedCompletions([]string{"debug", "info", "warn", "error"}, cobra.ShellCompDirectiveNoFileComp))
        cmd.Flags().BoolVarP(&r.buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
 
        // Configure local flags
        applyLocalFlagsBuild(cmd, r)
 
-       // Set bash-completion.
-       // Each flag must first be defined before using the SetAnnotation() call.
-       _ = cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
-
        return nil
 }
 
 // A sub set of the complete build flags. These flags are used by new and mod.
 func applyLocalFlagsBuildConfig(cmd *cobra.Command, r *rootCommand) {
        cmd.Flags().StringSliceP("theme", "t", []string{}, "themes to use (located in /themes/THEMENAME/)")
+       _ = cmd.MarkFlagDirname("theme")
        cmd.Flags().StringVarP(&r.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. https://spf13.com/")
        cmd.Flags().StringP("cacheDir", "", "", "filesystem path to cache directory")
-       _ = cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{})
+       _ = cmd.MarkFlagDirname("cacheDir")
        cmd.Flags().StringP("contentDir", "c", "", "filesystem path to content directory")
        cmd.Flags().StringSliceP("renderSegments", "", []string{}, "named segments to render (configured in the segments config)")
-       _ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"})
 }
 
 // Flags needed to do a build (used by hugo and hugo server commands)
@@ -535,8 +536,10 @@ func applyLocalFlagsBuild(cmd *cobra.Command, r *rootCommand) {
        cmd.Flags().BoolP("ignoreCache", "", false, "ignores the cache directory")
        cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date, author, and CODEOWNERS info to the pages")
        cmd.Flags().StringP("layoutDir", "l", "", "filesystem path to layout directory")
+       _ = cmd.MarkFlagDirname("layoutDir")
        cmd.Flags().BoolVar(&r.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build")
        cmd.Flags().StringVar(&r.poll, "poll", "", "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes")
+       _ = cmd.RegisterFlagCompletionFunc("poll", cobra.NoFileCompletions)
        cmd.Flags().Bool("panicOnWarning", false, "panic on first WARNING log")
        cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions")
        cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics")
@@ -559,8 +562,8 @@ func applyLocalFlagsBuild(cmd *cobra.Command, r *rootCommand) {
        cmd.Flags().MarkHidden("profile-mutex")
 
        cmd.Flags().StringSlice("disableKinds", []string{}, "disable different kind of pages (home, RSS etc.)")
+       _ = cmd.RegisterFlagCompletionFunc("disableKinds", cobra.FixedCompletions(kinds.AllKinds, cobra.ShellCompDirectiveNoFileComp))
        cmd.Flags().Bool("minify", false, "minify any supported output format (HTML, XML etc.)")
-       _ = cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{})
 }
 
 func (r *rootCommand) timeTrack(start time.Time, name string) {
index dfe54cba246f4b49fc13a5c58bb898436b16d26f..adf6bbe2bbd94738a1d15dd6c89c8a147c4d6799 100644 (file)
@@ -28,6 +28,7 @@ import (
        "github.com/gohugoio/hugo/modules"
        "github.com/gohugoio/hugo/parser"
        "github.com/gohugoio/hugo/parser/metadecoders"
+       "github.com/spf13/cobra"
 )
 
 // newConfigCommand creates a new config command and its subcommands.
@@ -112,7 +113,9 @@ func (c *configCommand) Init(cd *simplecobra.Commandeer) error {
        cmd.Short = "Print the site configuration"
        cmd.Long = `Print the site configuration, both default and custom settings.`
        cmd.Flags().StringVar(&c.format, "format", "toml", "preferred file format (toml, yaml or json)")
+       _ = cmd.RegisterFlagCompletionFunc("format", cobra.FixedCompletions([]string{"toml", "yaml", "json"}, cobra.ShellCompDirectiveNoFileComp))
        cmd.Flags().StringVar(&c.lang, "lang", "", "the language to display config for. Defaults to the first language defined.")
+       _ = cmd.RegisterFlagCompletionFunc("lang", cobra.NoFileCompletions)
        applyLocalFlagsBuildConfig(cmd, c.r)
 
        return nil
@@ -223,6 +226,7 @@ func (c *configMountsCommand) Init(cd *simplecobra.Commandeer) error {
        c.r = cd.Root.Command.(*rootCommand)
        cmd := cd.CobraCommand
        cmd.Short = "Print the configured file mounts"
+       cmd.ValidArgsFunction = cobra.NoFileCompletions
        applyLocalFlagsBuildConfig(cmd, c.r)
        return nil
 }
index c81ec792abced8b5deceb49b6cfc9359d9f4750e..4e1ceb7d11a0f2729c5cbb0f621b41f38e78eb8c 100644 (file)
@@ -46,6 +46,7 @@ to use JSON for the front matter.`,
                                        return c.convertContents(metadecoders.JSON)
                                },
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                },
                        },
                        &simpleCommand{
@@ -57,6 +58,7 @@ to use TOML for the front matter.`,
                                        return c.convertContents(metadecoders.TOML)
                                },
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                },
                        },
                        &simpleCommand{
@@ -68,6 +70,7 @@ to use YAML for the front matter.`,
                                        return c.convertContents(metadecoders.YAML)
                                },
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                },
                        },
                },
@@ -108,6 +111,7 @@ func (c *convertCommand) Init(cd *simplecobra.Commandeer) error {
 See convert's subcommands toJSON, toTOML and toYAML for more information.`
 
        cmd.PersistentFlags().StringVarP(&c.outputDir, "output", "o", "", "filesystem path to write files to")
+       _ = cmd.MarkFlagDirname("output")
        cmd.PersistentFlags().BoolVar(&c.unsafe, "unsafe", false, "enable less safe operations, please backup first")
 
        cmd.RunE = nil
index f5354f74d41dbea404766b261a97cda975bd5311..873da14a4a60f412a200e3627cb5ef985d50e472 100644 (file)
@@ -60,13 +60,17 @@ documentation.
                        return deployer.Deploy(ctx)
                },
                withc: func(cmd *cobra.Command, r *rootCommand) {
+                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                        cmd.Flags().String("target", "", "target deployment from deployments section in config file; defaults to the first one")
+                       _ = cmd.RegisterFlagCompletionFunc("target", cobra.NoFileCompletions)
                        cmd.Flags().Bool("confirm", false, "ask for confirmation before making changes to the target")
                        cmd.Flags().Bool("dryRun", false, "dry run")
                        cmd.Flags().Bool("force", false, "force upload of all files")
                        cmd.Flags().Bool("invalidateCDN", deployconfig.DefaultConfig.InvalidateCDN, "invalidate the CDN cache listed in the deployment target")
                        cmd.Flags().Int("maxDeletes", deployconfig.DefaultConfig.MaxDeletes, "maximum # of files to delete, or -1 to disable")
+                       _ = cmd.RegisterFlagCompletionFunc("maxDeletes", cobra.NoFileCompletions)
                        cmd.Flags().Int("workers", deployconfig.DefaultConfig.Workers, "number of workers to transfer files. defaults to 10")
+                       _ = cmd.RegisterFlagCompletionFunc("workers", cobra.NoFileCompletions)
                },
        }
 }
index 8e4f03c55b3bd17d58a45e4f3b7874dc65640b19..843fc49d1a95292d44d4dbd46d77415065c88e80 100644 (file)
@@ -19,6 +19,7 @@ import (
 
        "github.com/bep/simplecobra"
        "github.com/gohugoio/hugo/common/hugo"
+       "github.com/spf13/cobra"
 )
 
 func newEnvCommand() simplecobra.Commander {
@@ -47,6 +48,9 @@ func newEnvCommand() simplecobra.Commander {
                        }
                        return nil
                },
+               withc: func(cmd *cobra.Command, r *rootCommand) {
+                       cmd.ValidArgsFunction = cobra.NoFileCompletions
+               },
        }
 }
 
@@ -59,5 +63,8 @@ func newVersionCmd() simplecobra.Commander {
                },
                short: "Print Hugo version and environment info",
                long:  "Print Hugo version and environment info. This is useful in Hugo bug reports.",
+               withc: func(cmd *cobra.Command, r *rootCommand) {
+                       cmd.ValidArgsFunction = cobra.NoFileCompletions
+               },
        }
 }
index ae87091c4dc0cd62e08a1ec74cbf3d6030a7cde9..88eb1b6fc7f20f03d2c2dceac394b20e309a5369 100644 (file)
@@ -75,9 +75,13 @@ See https://xyproto.github.io/splash/docs/all.html for a preview of the availabl
                                return nil
                        },
                        withc: func(cmd *cobra.Command, r *rootCommand) {
+                               cmd.ValidArgsFunction = cobra.NoFileCompletions
                                cmd.PersistentFlags().StringVar(&style, "style", "friendly", "highlighter style (see https://xyproto.github.io/splash/docs/)")
+                               _ = cmd.RegisterFlagCompletionFunc("style", cobra.NoFileCompletions)
                                cmd.PersistentFlags().StringVar(&highlightStyle, "highlightStyle", "", "style used for highlighting lines (see https://github.com/alecthomas/chroma)")
+                               _ = cmd.RegisterFlagCompletionFunc("highlightStyle", cobra.NoFileCompletions)
                                cmd.PersistentFlags().StringVar(&linesStyle, "linesStyle", "", "style used for line numbers (see https://github.com/alecthomas/chroma)")
+                               _ = cmd.RegisterFlagCompletionFunc("linesStyle", cobra.NoFileCompletions)
                        },
                }
        }
@@ -115,9 +119,9 @@ See https://xyproto.github.io/splash/docs/all.html for a preview of the availabl
                                return nil
                        },
                        withc: func(cmd *cobra.Command, r *rootCommand) {
+                               cmd.ValidArgsFunction = cobra.NoFileCompletions
                                cmd.PersistentFlags().StringVar(&genmandir, "dir", "man/", "the directory to write the man pages.")
-                               // For bash-completion
-                               cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
+                               _ = cmd.MarkFlagDirname("dir")
                        },
                }
        }
@@ -172,9 +176,9 @@ url: %s
                                return nil
                        },
                        withc: func(cmd *cobra.Command, r *rootCommand) {
+                               cmd.ValidArgsFunction = cobra.NoFileCompletions
                                cmd.PersistentFlags().StringVar(&gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.")
-                               // For bash-completion
-                               cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
+                               _ = cmd.MarkFlagDirname("dir")
                        },
                }
        }
@@ -227,6 +231,7 @@ url: %s
                        },
                        withc: func(cmd *cobra.Command, r *rootCommand) {
                                cmd.Hidden = true
+                               cmd.ValidArgsFunction = cobra.NoFileCompletions
                                cmd.PersistentFlags().StringVarP(&docsHelperTarget, "dir", "", "docs/data", "data dir")
                        },
                }
index 947b6d11f9847244327a529a7e2406b03b9ee94a..c2d574aa17c88ba5aef9e1c53db128c11f04a93d 100644 (file)
@@ -58,6 +58,7 @@ Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root
                                        return c.importFromJekyll(args)
                                },
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        cmd.Flags().BoolVar(&c.force, "force", false, "allow import into non-empty target directory")
                                },
                        },
index 8debe5ca5f757577d4191b0514aa65234d54df89..b90a357f8ef616872099d788f6e8e32cb131295d 100644 (file)
@@ -26,6 +26,7 @@ import (
        "github.com/gohugoio/hugo/hugolib"
        "github.com/gohugoio/hugo/resources/page"
        "github.com/gohugoio/hugo/resources/resource"
+       "github.com/spf13/cobra"
 )
 
 // newListCommand creates a new list command and its subcommands.
@@ -102,6 +103,9 @@ func newListCommand() *listCommand {
                                                "buildExpired", true,
                                        )
                                },
+                               withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
+                               },
                        },
                        &simpleCommand{
                                name:  "future",
@@ -119,6 +123,9 @@ func newListCommand() *listCommand {
                                                "buildDrafts", true,
                                        )
                                },
+                               withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
+                               },
                        },
                        &simpleCommand{
                                name:  "expired",
@@ -136,6 +143,9 @@ func newListCommand() *listCommand {
                                                "buildDrafts", true,
                                        )
                                },
+                               withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
+                               },
                        },
                        &simpleCommand{
                                name:  "all",
@@ -147,6 +157,9 @@ func newListCommand() *listCommand {
                                        }
                                        return list(cd, r, shouldInclude, "buildDrafts", true, "buildFuture", true, "buildExpired", true)
                                },
+                               withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
+                               },
                        },
                },
        }
index d64d2a98321b9bb6b6a8259f192ed334774d4dad..7ff662944bf9aed541b96fdee527240e0c2c5f88 100644 (file)
@@ -62,6 +62,7 @@ removed from Hugo, but we need to test this out in "real life" to get a feel of
 so this may/will change in future versions of Hugo.
 `,
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        applyLocalFlagsBuildConfig(cmd, r)
                                },
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
@@ -89,6 +90,7 @@ so this may/will change in future versions of Hugo.
        inside a subfolder on GitHub, as one example.
        `,
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        applyLocalFlagsBuildConfig(cmd, r)
                                },
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
@@ -108,6 +110,7 @@ so this may/will change in future versions of Hugo.
                                short: "Verify dependencies.",
                                long:  `Verify checks that the dependencies of the current module, which are stored in a local downloaded source cache, have not been modified since being downloaded.`,
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        applyLocalFlagsBuildConfig(cmd, r)
                                        cmd.Flags().BoolVarP(&clean, "clean", "", false, "delete module cache for dependencies that fail verification")
                                },
@@ -127,6 +130,7 @@ so this may/will change in future versions of Hugo.
 Note that for vendored modules, that is the version listed and not the one from go.mod.
 `,
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        applyLocalFlagsBuildConfig(cmd, r)
                                        cmd.Flags().BoolVarP(&clean, "clean", "", false, "delete module cache for dependencies that fail verification")
                                },
@@ -144,8 +148,10 @@ Note that for vendored modules, that is the version listed and not the one from
                                short: "Delete the Hugo Module cache for the current project.",
                                long:  `Delete the Hugo Module cache for the current project.`,
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        applyLocalFlagsBuildConfig(cmd, r)
                                        cmd.Flags().StringVarP(&pattern, "pattern", "", "", `pattern matching module paths to clean (all if not set), e.g. "**hugo*"`)
+                                       _ = cmd.RegisterFlagCompletionFunc("pattern", cobra.NoFileCompletions)
                                        cmd.Flags().BoolVarP(&all, "all", "", false, "clean entire module cache")
                                },
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
@@ -167,6 +173,7 @@ Note that for vendored modules, that is the version listed and not the one from
                                name:  "tidy",
                                short: "Remove unused entries in go.mod and go.sum.",
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        applyLocalFlagsBuildConfig(cmd, r)
                                },
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
@@ -184,6 +191,7 @@ Note that for vendored modules, that is the version listed and not the one from
        If a module is vendored, that is where Hugo will look for it's dependencies.
        `,
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        applyLocalFlagsBuildConfig(cmd, r)
                                },
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
@@ -225,6 +233,7 @@ Run "go help get" for more information. All flags available for "go get" is also
 ` + commonUsageMod,
                                withc: func(cmd *cobra.Command, r *rootCommand) {
                                        cmd.DisableFlagParsing = true
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                },
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
                                        // We currently just pass on the flags we get to Go and
index 79d2c9e7edf8f5e2f239d8e9175ba518a5969ce8..144da7cd74d7ccefbc2ead8ec60e2c581abf9822 100644 (file)
@@ -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
+                               },
                        },
                },
        }
index 1d1aaad53d5419cfc86450fb733a08a4b80f03f2..b05226d35aabd482e2f6fe6d472e7ba790ba5d83 100644 (file)
@@ -43,9 +43,11 @@ func newReleaseCommand() simplecobra.Commander {
                },
                withc: func(cmd *cobra.Command, r *rootCommand) {
                        cmd.Hidden = true
+                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                        cmd.PersistentFlags().BoolVarP(&skipPush, "skip-push", "", false, "skip pushing to remote")
                        cmd.PersistentFlags().BoolVarP(&try, "try", "", false, "no changes")
                        cmd.PersistentFlags().IntVarP(&step, "step", "", 0, "step to run (1: set new version 2: prepare next dev version)")
+                       _ = cmd.RegisterFlagCompletionFunc("step", cobra.FixedCompletions([]string{"1", "2"}, cobra.ShellCompDirectiveNoFileComp))
                },
        }
 }
index 870283bbad133128f8ce56a8c974c9b29ef18566..09fb4e79f7e1275535fd03d06a76aca8cddaef35 100644 (file)
@@ -123,6 +123,7 @@ func newServerCommand() *serverCommand {
                                        return mclib.RunMain()
                                },
                                withc: func(cmd *cobra.Command, r *rootCommand) {
+                                       cmd.ValidArgsFunction = cobra.NoFileCompletions
                                        cmd.Flags().BoolVar(&uninstall, "uninstall", false, "Uninstall the local CA (but do not delete it).")
                                },
                        },
@@ -523,10 +524,15 @@ of a second, you will be able to save and see your changes nearly instantly.`
        cmd.Aliases = []string{"serve"}
 
        cmd.Flags().IntVarP(&c.serverPort, "port", "p", 1313, "port on which the server will listen")
+       _ = cmd.RegisterFlagCompletionFunc("port", cobra.NoFileCompletions)
        cmd.Flags().IntVar(&c.liveReloadPort, "liveReloadPort", -1, "port for live reloading (i.e. 443 in HTTPS proxy situations)")
+       _ = cmd.RegisterFlagCompletionFunc("liveReloadPort", cobra.NoFileCompletions)
        cmd.Flags().StringVarP(&c.serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")
+       _ = cmd.RegisterFlagCompletionFunc("bind", cobra.NoFileCompletions)
        cmd.Flags().StringVarP(&c.tlsCertFile, "tlsCertFile", "", "", "path to TLS certificate file")
+       _ = cmd.MarkFlagFilename("tlsCertFile", "pem")
        cmd.Flags().StringVarP(&c.tlsKeyFile, "tlsKeyFile", "", "", "path to TLS key file")
+       _ = cmd.MarkFlagFilename("tlsKeyFile", "pem")
        cmd.Flags().BoolVar(&c.tlsAuto, "tlsAuto", false, "generate and use locally-trusted certificates.")
        cmd.Flags().BoolVar(&c.pprof, "pprof", false, "enable the pprof server (port 8080)")
        cmd.Flags().BoolVarP(&c.serverWatch, "watch", "w", true, "watch filesystem for changes and recreate as needed")
@@ -538,9 +544,6 @@ of a second, you will be able to save and see your changes nearly instantly.`
        cmd.Flags().BoolVar(&c.disableFastRender, "disableFastRender", false, "enables full re-renders on changes")
        cmd.Flags().BoolVar(&c.disableBrowserError, "disableBrowserError", false, "do not show build errors in the browser")
 
-       cmd.Flags().SetAnnotation("tlsCertFile", cobra.BashCompSubdirsInDir, []string{})
-       cmd.Flags().SetAnnotation("tlsKeyFile", cobra.BashCompSubdirsInDir, []string{})
-
        r := cd.Root.Command.(*rootCommand)
        applyLocalFlagsBuild(cmd, r)