Skip to content

Commit

Permalink
Add helpers.NormalizeHugoFlagsFunc() to handle flag name changes
Browse files Browse the repository at this point in the history
It currently handles --baseUrl to --baseURL, and --uglyUrls to --uglyURLs.

Special thanks to Eric Paris (@eparis) for writing the
"normalized name" support in Cobra, and for showing us
how it is used in Kubernetes.

See Issue gohugoio#959
  • Loading branch information
anthonyfok committed Sep 9, 2015
1 parent cf7318a commit d680f2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var Source, CacheDir, Destination, Theme, BaseURL, CfgFile, LogFile, Editor stri

//Execute adds all child commands to the root command HugoCmd and sets flags appropriately.
func Execute() {
HugoCmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlagsFunc)
AddCommands()
utils.StopOnErr(HugoCmd.Execute())
}
Expand Down Expand Up @@ -94,8 +95,8 @@ func init() {
HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to")
HugoCmd.PersistentFlags().StringVarP(&Theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
HugoCmd.PersistentFlags().BoolVar(&UglyURLs, "uglyUrls", false, "if true, use /filename.html instead of /filename/")
HugoCmd.PersistentFlags().StringVarP(&BaseURL, "baseUrl", "b", "", "hostname (and path) to the root eg. http://spf13.com/")
HugoCmd.PersistentFlags().BoolVar(&UglyURLs, "uglyURLs", false, "if true, use /filename.html instead of /filename/")
HugoCmd.PersistentFlags().StringVarP(&BaseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/")
HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
HugoCmd.PersistentFlags().StringVar(&Editor, "editor", "", "edit new content with this editor, if provided")
HugoCmd.PersistentFlags().BoolVar(&Logging, "log", false, "Enable Logging")
Expand Down Expand Up @@ -189,7 +190,7 @@ func InitializeConfig() {
viper.Set("BuildFuture", Future)
}

if hugoCmdV.PersistentFlags().Lookup("uglyUrls").Changed {
if hugoCmdV.PersistentFlags().Lookup("uglyURLs").Changed {
viper.Set("UglyURLs", UglyURLs)
}

Expand Down
15 changes: 15 additions & 0 deletions helpers/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/spf13/cast"
bp "github.com/spf13/hugo/bufferpool"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -428,3 +429,17 @@ func DoArithmetic(a, b interface{}, op rune) (interface{}, error) {
return nil, errors.New("There is no such an operation")
}
}

// NormalizeHugoFlagsFunc facilitates transitions of Hugo command-line flags,
// e.g. --baseUrl to --baseURL, --uglyUrls to --uglyURLs
func NormalizeHugoFlagsFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name {
case "baseUrl":
name = "baseURL"
break
case "uglyUrls":
name = "uglyURLs"
break
}
return pflag.NormalizedName(name)
}

0 comments on commit d680f2d

Please sign in to comment.