summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorMartin Treusch von Buttlar <martin.tvb@vitraum.de>2017-05-22 09:00:23 +0200
committerMartin Treusch von Buttlar <martin.tvb@vitraum.de>2017-05-22 09:00:23 +0200
commit9f4c550b8be0f80fd670f7ded32dbd4aab8d1f4e (patch)
treed4b3ef3ec0ac61a210ca079861e1334ca29a70c8 /cmd
parent4c03187fbef64573ded62f40d5d4dace6c48747b (diff)
add help flags for sub commands to root cmd
Diffstat (limited to 'cmd')
-rw-r--r--cmd/ponzu/add.go2
-rw-r--r--cmd/ponzu/build.go2
-rw-r--r--cmd/ponzu/generate.go2
-rw-r--r--cmd/ponzu/main.go3
-rw-r--r--cmd/ponzu/new.go2
-rw-r--r--cmd/ponzu/upgrade.go2
-rw-r--r--cmd/ponzu/usage.go121
-rw-r--r--cmd/ponzu/version.go64
8 files changed, 148 insertions, 50 deletions
diff --git a/cmd/ponzu/add.go b/cmd/ponzu/add.go
index db8aa2e..30cf168 100644
--- a/cmd/ponzu/add.go
+++ b/cmd/ponzu/add.go
@@ -177,5 +177,5 @@ func addError(err error) error {
}
func init() {
- rootCmd.AddCommand(addCmd)
+ RegisterCmdlineCommand(addCmd)
}
diff --git a/cmd/ponzu/build.go b/cmd/ponzu/build.go
index 890ccbe..0037355 100644
--- a/cmd/ponzu/build.go
+++ b/cmd/ponzu/build.go
@@ -54,5 +54,5 @@ $ ponzu -gocmd=go1.8rc1 build`,
}
func init() {
- rootCmd.AddCommand(buildCmd)
+ RegisterCmdlineCommand(buildCmd)
}
diff --git a/cmd/ponzu/generate.go b/cmd/ponzu/generate.go
index 048addd..bc94913 100644
--- a/cmd/ponzu/generate.go
+++ b/cmd/ponzu/generate.go
@@ -450,5 +450,5 @@ var contentCmd = &cobra.Command{
func init() {
generateCmd.AddCommand(contentCmd)
- rootCmd.AddCommand(generateCmd)
+ RegisterCmdlineCommand(generateCmd)
}
diff --git a/cmd/ponzu/main.go b/cmd/ponzu/main.go
index 6d8c974..fc9757d 100644
--- a/cmd/ponzu/main.go
+++ b/cmd/ponzu/main.go
@@ -185,7 +185,8 @@ func init() {
cmd.Flags().BoolVar(&devhttps, "devhttps", false, "[dev environment] enable automatic TLS/SSL certificate management")
}
- rootCmd.AddCommand(runCmd, serveCmd)
+ RegisterCmdlineCommand(runCmd)
+ RegisterCmdlineCommand(serveCmd)
pflags := rootCmd.PersistentFlags()
pflags.StringVar(&gocmd, "gocmd", "go", "custom go command if using beta or new release of Go")
diff --git a/cmd/ponzu/new.go b/cmd/ponzu/new.go
index 0a19da1..9911931 100644
--- a/cmd/ponzu/new.go
+++ b/cmd/ponzu/new.go
@@ -195,5 +195,5 @@ func init() {
newCmd.Flags().StringVar(&fork, "fork", "", "modify repo source for Ponzu core development")
newCmd.Flags().BoolVar(&dev, "dev", false, "modify environment for Ponzu core development")
- rootCmd.AddCommand(newCmd)
+ RegisterCmdlineCommand(newCmd)
}
diff --git a/cmd/ponzu/upgrade.go b/cmd/ponzu/upgrade.go
index 2bcbc53..9671c79 100644
--- a/cmd/ponzu/upgrade.go
+++ b/cmd/ponzu/upgrade.go
@@ -163,5 +163,5 @@ func upgradePonzuProjectDir(path string) error {
}
func init() {
- rootCmd.AddCommand(upgradeCmd)
+ RegisterCmdlineCommand(upgradeCmd)
}
diff --git a/cmd/ponzu/usage.go b/cmd/ponzu/usage.go
index 3046be7..9a78587 100644
--- a/cmd/ponzu/usage.go
+++ b/cmd/ponzu/usage.go
@@ -1,64 +1,97 @@
package main
import (
- "encoding/json"
"fmt"
- "io/ioutil"
- "os"
- "path/filepath"
+ "sort"
+ "strings"
+ "text/template"
+ "unicode"
"github.com/spf13/cobra"
)
-var versionCmd = &cobra.Command{
- Use: "version",
- Aliases: []string{"v"},
- Short: "Prints the version of Ponzu your project is using.",
- Long: `Prints the version of Ponzu your project is using. Must be called from
-within a Ponzu project directory.`,
- Example: `$ ponzu version
-> Ponzu v0.7.1
-(or)
-$ ponzu --cli version
-> Ponzu v0.7.2`,
+var templateFuncs = template.FuncMap{
+ "rpad": rpad,
+ "trimTrailingWhitespaces": trimRightSpace,
+}
+
+var tmpl = `{{with (or .Cmd.Long .Cmd.Short)}}{{. | trimTrailingWhitespaces}}
+
+{{end}}{{if or .Cmd.Runnable .Cmd.HasSubCommands}}Usage:{{if .Cmd.Runnable}}
+ {{.Cmd.UseLine}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
+ {{.Cmd.CommandPath}} [command]{{end}}{{if gt (len .Cmd.Aliases) 0}}
+
+Aliases:
+ {{.Cmd.NameAndAliases}}{{end}}{{if .Cmd.HasExample}}
+
+Examples:
+{{.Cmd.Example}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
+
+Available Commands:{{range .Cmd.Commands}}{{if (or .IsAvailableCommand false)}}
+ {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .Cmd.HasAvailableLocalFlags}}
+
+Flags for all commands:
+{{.Cmd.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{range .Subs}}{{if (and .IsAvailableCommand .HasAvailableLocalFlags)}}
+
+Flags for '{{.Name}}' command:
+{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{end}}{{if .Cmd.HasHelpSubCommands}}
+
+Additional help topics:{{range .Cmd.Commands}}{{if .Cmd.IsAdditionalHelpTopicCommand}}
+ {{rpad .Cmd.CommandPath .Cmd.CommandPathPadding}} {{.Cmd.Short}}{{end}}{{end}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
+
+Use "{{.Cmd.CommandPath}} [command] --help" for more information about a command.{{end}}
+{{end}}`
+
+var helpCmd = &cobra.Command{
+ Use: "help",
+ Short: "help about any command",
Run: func(cmd *cobra.Command, args []string) {
- p, err := ponzu(cli)
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
+ cmd, _, e := rootCmd.Find(args)
+ if cmd == nil || e != nil {
+ rootCmd.Printf("Unknown help topic %#q\n", args)
+ rootCmd.Usage()
+ return
+ }
+ t := template.New("help")
+ t.Funcs(templateFuncs)
+ template.Must(t.Parse(tmpl))
+ if len(args) > 0 {
+ rootCmd.HelpFunc()(cmd, args)
+ return
}
- fmt.Fprintf(os.Stdout, "Ponzu v%s\n", p["version"])
- },
-}
-
-func ponzu(isCLI bool) (map[string]interface{}, error) {
- kv := make(map[string]interface{})
+ sortByName := func(i, j int) bool { return cmds[i].Name() < cmds[j].Name() }
+ sort.Slice(cmds, sortByName)
- info := filepath.Join("cmd", "ponzu", "ponzu.json")
- if isCLI {
- gopath, err := getGOPATH()
+ err := t.Execute(cmd.OutOrStdout(), struct {
+ Cmd *cobra.Command
+ Subs []*cobra.Command
+ }{
+ Cmd: rootCmd,
+ Subs: cmds})
if err != nil {
- return nil, err
+ cmd.Println(err)
}
- repo := filepath.Join(gopath, "src", "github.com", "ponzu-cms", "ponzu")
- info = filepath.Join(repo, "cmd", "ponzu", "ponzu.json")
- }
-
- b, err := ioutil.ReadFile(info)
- if err != nil {
- return nil, err
- }
+ },
+}
- err = json.Unmarshal(b, &kv)
- if err != nil {
- return nil, err
- }
+var cmds []*cobra.Command
- return kv, nil
+func RegisterCmdlineCommand(cmd *cobra.Command) {
+ rootCmd.AddCommand(cmd)
+ cmds = append(cmds, cmd)
}
func init() {
- versionCmd.Flags().BoolVar(&cli, "cli", false, "specify that information should be returned about the CLI, not project")
- rootCmd.AddCommand(versionCmd)
+ rootCmd.AddCommand(helpCmd)
+}
+
+// rpad adds padding to the right of a string.
+func rpad(s string, padding int) string {
+ template := fmt.Sprintf("%%-%ds", padding)
+ return fmt.Sprintf(template, s)
+}
+
+func trimRightSpace(s string) string {
+ return strings.TrimRightFunc(s, unicode.IsSpace)
}
diff --git a/cmd/ponzu/version.go b/cmd/ponzu/version.go
new file mode 100644
index 0000000..d22d5a4
--- /dev/null
+++ b/cmd/ponzu/version.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+
+ "github.com/spf13/cobra"
+)
+
+var versionCmd = &cobra.Command{
+ Use: "version",
+ Aliases: []string{"v"},
+ Short: "Prints the version of Ponzu your project is using.",
+ Long: `Prints the version of Ponzu your project is using. Must be called from
+within a Ponzu project directory.`,
+ Example: `$ ponzu version
+> Ponzu v0.7.1
+(or)
+$ ponzu --cli version
+> Ponzu v0.7.2`,
+ Run: func(cmd *cobra.Command, args []string) {
+ p, err := version(cli)
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+
+ fmt.Fprintf(os.Stdout, "Ponzu v%s\n", p["version"])
+ },
+}
+
+func version(isCLI bool) (map[string]interface{}, error) {
+ kv := make(map[string]interface{})
+
+ info := filepath.Join("cmd", "ponzu", "ponzu.json")
+ if isCLI {
+ gopath, err := getGOPATH()
+ if err != nil {
+ return nil, err
+ }
+ repo := filepath.Join(gopath, "src", "github.com", "ponzu-cms", "ponzu")
+ info = filepath.Join(repo, "cmd", "ponzu", "ponzu.json")
+ }
+
+ b, err := ioutil.ReadFile(info)
+ if err != nil {
+ return nil, err
+ }
+
+ err = json.Unmarshal(b, &kv)
+ if err != nil {
+ return nil, err
+ }
+
+ return kv, nil
+}
+
+func init() {
+ versionCmd.Flags().BoolVar(&cli, "cli", false, "specify that information should be returned about the CLI, not project")
+ RegisterCmdlineCommand(versionCmd)
+}