@cryptotaxi247 / kubo / commits / 7666f8880

commands: Allow overriding helptext sections with hand-written strings

Matt Bell committed Nov 11, 2014 at 18:44 UTC 7666f8880c7e3e43b6a93f32a6e481e928ffd80b
2 files changed +31 -8
commands/cli/helptext.go
+24 -6
@@ -36,18 +36,36 @@ func HelpText(rootName string, root *cmds.Command, path []string) (string, error
36 }
37
38 if cmd.Arguments != nil {
39 - lines := indent(argumentText(cmd), " ")
40 - s += fmt.Sprintf("Arguments:\n%v\n\n", strings.Join(lines, "\n"))
39 + if len(cmd.ArgumentHelp) > 0 {
40 + s += cmd.ArgumentHelp
41 + } else {
42 + section := strings.Join(indent(argumentText(cmd), " "), "\n")
43 + s += fmt.Sprintf("Arguments:\n%v", section)
44 + }
45 +
46 + s += "\n\n"
47 }
48
49 if cmd.Subcommands != nil {
44 - lines := indent(subcommandText(cmd, rootName, path), " ")
45 - s += fmt.Sprintf("Subcommands:\n%v\n\n", strings.Join(lines, "\n"))
50 + if len(cmd.SubcommandHelp) > 0 {
51 + s += cmd.SubcommandHelp
52 + } else {
53 + section := strings.Join(indent(subcommandText(cmd, rootName, path), " "), "\n")
54 + s += fmt.Sprintf("Subcommands:\n%v", section)
55 + }
56 +
57 + s += "\n\n"
58 }
59
60 if cmd.Options != nil {
49 - lines := indent(optionText(cmd), " ")
50 - s += fmt.Sprintf("Options:\n%v\n\n", strings.Join(lines, "\n"))
61 + if len(cmd.OptionHelp) > 0 {
62 + s += cmd.OptionHelp
63 + } else {
64 + section := strings.Join(indent(optionText(cmd), " "), "\n")
65 + s += fmt.Sprintf("Options:\n%v", section)
66 + }
67 +
68 + s += "\n\n"
69 }
70
71 return s, nil
commands/command.go
+7 -2
@@ -28,8 +28,13 @@ type Marshaller func(Response) ([]byte, error)
28 // Command is a runnable command, with input arguments and options (flags).
29 // It can also have Subcommands, to group units of work into sets.
30 type Command struct {
31 - Description string
32 - Help string
31 + // MAYBE_TODO: move all the text fields into a struct
32 + // MAYBE_TODO: move these out of command and put them somewhere in commands/cli
33 + Description string
34 + Help string
35 + SubcommandHelp string
36 + OptionHelp string
37 + ArgumentHelp string
38
39 Options []Option
40 Arguments []Argument