From 1f326ad914f749b3558c40c5410b35cb01299ed9 Mon Sep 17 00:00:00 2001 From: Anthony Fok Date: Sat, 6 Feb 2016 23:31:20 +0800 Subject: [PATCH] Fix bash-completion for Hugo builder flags such as --theme by placing their SetAnnotation() calls after their definitions. See #1824 and 87ca0d0 --- commands/hugo.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/commands/hugo.go b/commands/hugo.go index 652e9f040..8a45fbe3c 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -188,26 +188,23 @@ func AddCommands() { } // initHugoBuilderFlags initializes all common flags, typically used by the -// core build commands. +// core build commands, namely hugo itself, server, check and benchmark. func initHugoBuilderFlags(cmd *cobra.Command) { initCoreCommonFlags(cmd) initHugoBuildCommonFlags(cmd) } -// initCoreCommonFlags initializes common flags used by Hugo core commands -// such as hugo itself, server, check, config and benchmark. +// initCoreCommonFlags initializes common flags used by Hugo core commands. func initCoreCommonFlags(cmd *cobra.Command) { cmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is path/config.yaml|json|toml)") - // For bash-completion + + // Set bash-completion validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"} cmd.Flags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames) - cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) - cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{}) - cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{}) - cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"}) } -// initial flags related to the Hugo build +// initHugoBuildCommonFlags initialize common flags related to the Hugo build. +// Called by initHugoBuilderFlags. func initHugoBuildCommonFlags(cmd *cobra.Command) { cmd.Flags().BoolVar(&cleanDestination, "cleanDestinationDir", false, "Remove files from destination not found in static directories") cmd.Flags().BoolVarP(&draft, "buildDrafts", "D", false, "include content marked as draft") @@ -230,6 +227,12 @@ func initHugoBuildCommonFlags(cmd *cobra.Command) { cmd.Flags().BoolVarP(&forceSync, "forceSyncStatic", "", false, "Copy all files when static is changed.") cmd.Flags().BoolVarP(&noTimes, "noTimes", "", false, "Don't sync modification time of files") + // Set bash-completion. + // Each flag must first be defined before using the SetAnnotation() call. + cmd.Flags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) + cmd.Flags().SetAnnotation("cacheDir", cobra.BashCompSubdirsInDir, []string{}) + cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{}) + cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"}) } func initBenchmarkBuildingFlags(cmd *cobra.Command) { @@ -249,7 +252,7 @@ func init() { HugoCmd.Flags().BoolVarP(&buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed") hugoCmdV = HugoCmd - // For bash-completion + // Set bash-completion HugoCmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{}) }