helptext.go 6.06 KB
Newer Older
1 2 3 4
package cli

import (
	"fmt"
5
	"io"
6
	"strings"
7
	"text/template"
8 9 10 11 12 13 14 15 16 17 18 19

	cmds "github.com/jbenet/go-ipfs/commands"
)

const (
	requiredArg = "<%v>"
	optionalArg = "[<%v>]"
	variadicArg = "%v..."
	optionFlag  = "-%v"
	optionType  = "(%v)"

	whitespace = "\r\n\t "
20 21

	indentStr = "    "
22 23
)

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
type helpFields struct {
	Indent      string
	Path        string
	ArgUsage    string
	Tagline     string
	Arguments   string
	Options     string
	Subcommands string
	Description string
}

const usageFormat = "{{.Path}}{{if .ArgUsage}} {{.ArgUsage}}{{end}} - {{.Tagline}}"

const longHelpFormat = `
{{.Indent}}{{template "usage" .}}

{{if .Arguments}}ARGUMENTS:

{{.Indent}}{{.Arguments}}

{{end}}{{if .Options}}OPTIONS:

{{.Indent}}{{.Options}}

{{end}}{{if .Subcommands}}SUBCOMMANDS:

{{.Indent}}{{.Subcommands}}

{{.Indent}}Use '{{.Path}} <subcmd> --help' for more information about each command.

{{end}}{{if .Description}}DESCRIPTION:

{{.Indent}}{{.Description}}

{{end}}
`
60 61 62 63 64 65 66
const shortHelpFormat = `USAGE:
{{.Indent}}{{template "usage" .}}
{{if .Description}}
{{.Indent}}{{.Description}}
{{end}}
Use '{{.Path}} --help' for more information about this command.
`
67 68

var usageTemplate *template.Template
69 70
var longHelpTemplate *template.Template
var shortHelpTemplate *template.Template
71 72 73

func init() {
	tmpl, err := template.New("usage").Parse(usageFormat)
74
	if err != nil {
75
		panic(err)
76
	}
77
	usageTemplate = tmpl
78

79 80 81
	tmpl, err = usageTemplate.New("longHelp").Parse(longHelpFormat)
	if err != nil {
		panic(err)
82
	}
83
	longHelpTemplate = tmpl
84 85 86 87 88 89

	tmpl, err = usageTemplate.New("shortHelp").Parse(shortHelpFormat)
	if err != nil {
		panic(err)
	}
	shortHelpTemplate = tmpl
90
}
91

92 93 94 95 96
// LongHelp returns a formatted CLI helptext string, generated for the given command
func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer) error {
	cmd, err := root.Get(path)
	if err != nil {
		return err
97 98
	}

99 100 101
	pathStr := rootName
	if len(path) > 0 {
		pathStr += " " + strings.Join(path, " ")
102 103
	}

104 105 106 107 108 109 110 111 112
	fields := helpFields{
		Indent:      indentStr,
		Path:        pathStr,
		ArgUsage:    usageText(cmd),
		Tagline:     cmd.Description,
		Arguments:   cmd.ArgumentHelp,
		Options:     cmd.OptionHelp,
		Subcommands: cmd.SubcommandHelp,
		Description: cmd.Help,
113 114
	}

115 116 117
	// autogen fields that are empty
	if len(cmd.ArgumentHelp) == 0 {
		fields.Arguments = strings.Join(argumentText(cmd), "\n")
118
	}
119 120 121 122 123 124 125 126 127 128 129
	if len(cmd.OptionHelp) == 0 {
		fields.Options = strings.Join(optionText(cmd), "\n")
	}
	if len(cmd.SubcommandHelp) == 0 {
		fields.Subcommands = strings.Join(subcommandText(cmd, rootName, path), "\n")
	}

	fields.Arguments = indentString(fields.Arguments, indentStr)
	fields.Options = indentString(fields.Options, indentStr)
	fields.Subcommands = indentString(fields.Subcommands, indentStr)
	fields.Description = indentString(fields.Description, indentStr)
130

131
	return longHelpTemplate.Execute(out, fields)
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
// ShortHelp returns a formatted CLI helptext string, generated for the given command
func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer) error {
	cmd, err := root.Get(path)
	if err != nil {
		return err
	}

	pathStr := rootName
	if len(path) > 0 {
		pathStr += " " + strings.Join(path, " ")
	}

	fields := helpFields{
		Indent:      indentStr,
		Path:        pathStr,
		ArgUsage:    usageText(cmd),
		Tagline:     cmd.Description,
		Description: cmd.Help,
	}

	fields.Description = indentString(fields.Description, indentStr)

	return shortHelpTemplate.Execute(out, fields)
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
func argumentText(cmd *cmds.Command) []string {
	lines := make([]string, len(cmd.Arguments))

	for i, arg := range cmd.Arguments {
		lines[i] = argUsageText(arg)
		lines[i] += "\n" + arg.Description
		lines[i] = indentString(lines[i], "    ") + "\n"
	}

	return lines
}

func optionText(cmd ...*cmds.Command) []string {
	// get a slice of the options we want to list out
	options := make([]cmds.Option, 0)
	for _, c := range cmd {
		for _, opt := range c.Options {
			options = append(options, opt)
		}
	}

	// add option names to output (with each name aligned)
	lines := make([]string, 0)
	j := 0
	for {
		done := true
		i := 0
		for _, opt := range options {
			if len(lines) < i+1 {
				lines = append(lines, "")
			}
			if len(opt.Names) >= j+1 {
				lines[i] += fmt.Sprintf(optionFlag, opt.Names[j])
			}
			if len(opt.Names) > j+1 {
				lines[i] += ", "
				done = false
			}

			i++
		}

		if done {
			break
		}

		lines = align(lines)
		j++
	}

	// add option types to output
	for i, opt := range options {
		lines[i] += " " + fmt.Sprintf(optionType, opt.Type)
	}
	lines = align(lines)

	// add option descriptions to output
	for i, opt := range options {
		lines[i] += "\n" + opt.Description
		lines[i] = indentString(lines[i], "    ") + "\n"
	}

	return lines
}

func subcommandText(cmd *cmds.Command, rootName string, path []string) []string {
	prefix := fmt.Sprintf("%v %v", rootName, strings.Join(path, " "))
226 227 228
	if len(path) > 0 {
		prefix += " "
	}
229 230 231 232 233
	lines := make([]string, len(cmd.Subcommands))

	i := 0
	for name, sub := range cmd.Subcommands {
		usage := usageText(sub)
234
		lines[i] = fmt.Sprintf("%v%v %v", prefix, name, usage)
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		lines[i] += fmt.Sprintf("\n%v", sub.Description)
		lines[i] = indentString(lines[i], "    ") + "\n"
		i++
	}

	return lines
}

func usageText(cmd *cmds.Command) string {
	s := ""
	for i, arg := range cmd.Arguments {
		if i != 0 {
			s += " "
		}
		s += argUsageText(arg)
	}

	return s
}

func argUsageText(arg cmds.Argument) string {
	s := arg.Name

	if arg.Required {
		s = fmt.Sprintf(requiredArg, s)
	} else {
		s = fmt.Sprintf(optionalArg, s)
	}

	if arg.Variadic {
		s = fmt.Sprintf(variadicArg, s)
	}

	return s
}

func align(lines []string) []string {
	longest := 0
	for _, line := range lines {
		length := len(line)
		if length > longest {
			longest = length
		}
	}

	for i, line := range lines {
		length := len(line)
		if length > 0 {
			lines[i] += strings.Repeat(" ", longest-length)
		}
	}

	return lines
}

func indent(lines []string, prefix string) []string {
	for i, line := range lines {
		lines[i] = prefix + indentString(line, prefix)
	}
	return lines
}

func indentString(line string, prefix string) string {
	return strings.Replace(line, "\n", "\n"+prefix, -1)
}