Skip to content

Commit

Permalink
better formatting for help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jrperritt committed Aug 2, 2015
1 parent aca4049 commit 943cbd1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
2 changes: 1 addition & 1 deletion commandoptions/commandflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func CommandFlags(f func() []cli.Flag, keys []string) []cli.Flag {
}
flagFields := cli.StringFlag{
Name: "fields",
Usage: fmt.Sprintf("Only return these comma-separated case-insensitive fields.\n\tChoices: %s", strings.Join(fields, ", ")),
Usage: fmt.Sprintf("[optional] Only return these comma-separated case-insensitive fields.\n\tChoices: %s", strings.Join(fields, ", ")),
}
of = append(of, flagFields)
}
Expand Down
86 changes: 62 additions & 24 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,19 @@ package main

import (
"fmt"
"html/template"
"io"
"strings"
"text/tabwriter"
"text/template"

"github.com/jrperritt/rack/commandoptions"
"github.com/jrperritt/rack/internal/github.com/codegangsta/cli"
)

var commandHelpTemplate = `NAME: {{.Name}} - {{.Usage}}{{if .Description}}
DESCRIPTION: {{.Description}}{{end}}{{if .Flags}}
OPTIONS:
{{range .Flags}}{{flag .}}
{{end}}{{ end }}
`

var appHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} <command> <subcommand> <action> [OPTIONS]
{{.Name}} <command> <subcommand> <action> [FLAGS]
{{if .Version}}
VERSION:
{{.Version}}
Expand All @@ -32,10 +24,35 @@ COMMANDS:
{{end}}{{end}}
`

var commandHelpTemplate = `NAME: {{.Name}} - {{.Usage}}{{if .Description}}
DESCRIPTION: {{.Description}}{{end}}{{if .Flags}}
COMMAND FLAGS:
{{range .Flags}}{{if isNotGlobalFlag .}}{{flag .}}
{{end}}{{end}}
GLOBAL FLAGS:
{{range .Flags}}{{if isGlobalFlag .}}{{flag .}}
{{end}}{{end}}{{ end }}
`

var subcommandHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}}{{if eq (len (split .Name " ")) 2}} <subcommand>{{end}} <action> [FLAGS]
{{if eq (len (split .Name " ")) 2}}SUBCOMMANDS{{else}}ACTIONS{{end}}:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}
`

func printHelp(out io.Writer, templ string, data interface{}) {
funcMap := template.FuncMap{
"join": strings.Join,
"flag": flag,
"split": strings.Split,
"join": strings.Join,
"isGlobalFlag": isGlobalFlag,
"isNotGlobalFlag": isNotGlobalFlag,
"flag": flag,
}

w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0)
Expand All @@ -47,20 +64,41 @@ func printHelp(out io.Writer, templ string, data interface{}) {
w.Flush()
}

func flag(flag cli.Flag) string {
switch flag.(type) {
func isGlobalFlag(cliflag cli.Flag) bool {
globalFlags := commandoptions.GlobalFlags()
for _, globalFlag := range globalFlags {
if globalFlag == cliflag {
return true
}
}
return false
}

func isNotGlobalFlag(cliflag cli.Flag) bool {
globalFlags := commandoptions.GlobalFlags()
for _, globalFlag := range globalFlags {
if globalFlag == cliflag {
return false
}
}
return true
}

func flag(cliflag cli.Flag) string {
var flagString string
switch cliflag.(type) {
case cli.StringFlag:
flagType := flag.(cli.StringFlag)
return fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
flagType := cliflag.(cli.StringFlag)
flagString = fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
case cli.IntFlag:
flagType := flag.(cli.IntFlag)
return fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
flagType := cliflag.(cli.IntFlag)
flagString = fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
case cli.BoolFlag:
flagType := flag.(cli.BoolFlag)
return fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
flagType := cliflag.(cli.BoolFlag)
flagString = fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
case cli.StringSliceFlag:
flagType := flag.(cli.StringSliceFlag)
return fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
flagType := cliflag.(cli.StringSliceFlag)
flagString = fmt.Sprintf("%s\t%s", flagType.Name, flagType.Usage)
}
return ""
return flagString
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (

func main() {
cli.HelpPrinter = printHelp
cli.CommandHelpTemplate = commandHelpTemplate
cli.AppHelpTemplate = appHelpTemplate
cli.CommandHelpTemplate = commandHelpTemplate
cli.SubcommandHelpTemplate = subcommandHelpTemplate
app := cli.NewApp()
app.Name = "rack"
app.Version = fmt.Sprintf("%v version %v\n commit: %v\n", app.Name, util.Version, util.Commit)
Expand Down
2 changes: 1 addition & 1 deletion util/commit.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package util

var Commit = "cac851bbf53f52b597288da301bdf9feebe6eb1b"
var Commit = "f679abdfec517729850002474aacc9e800e52249"

0 comments on commit 943cbd1

Please sign in to comment.