@cryptotaxi247 / kubo / commits / 206529730

Add Synopsis autogenerator

Generates synopsis automagically License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Jun 1, 2016 at 20:15 UTC 206529730a2a132c548e41e7c1ba773adc49c930
4 files changed +52 -19
commands/cli/helptext.go
+44
@@ -163,6 +163,9 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
163 if len(fields.Subcommands) == 0 {
164 fields.Subcommands = strings.Join(subcommandText(cmd, rootName, path), "\n")
165 }
166 + if len(fields.Synopsis) == 0 {
167 + fields.Synopsis = generateSynopsis(cmd, pathStr)
168 + }
169
170 // trim the extra newlines (see TrimNewlines doc)
171 fields.TrimNewlines()
@@ -206,6 +209,9 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
209 if len(fields.Subcommands) == 0 {
210 fields.Subcommands = strings.Join(subcommandText(cmd, rootName, path), "\n")
211 }
212 + if len(fields.Synopsis) == 0 {
213 + fields.Synopsis = generateSynopsis(cmd, pathStr)
214 + }
215
216 // trim the extra newlines (see TrimNewlines doc)
217 fields.TrimNewlines()
@@ -216,6 +222,44 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
222 return shortHelpTemplate.Execute(out, fields)
223 }
224
225 +func generateSynopsis(cmd *cmds.Command, path string) string {
226 + res := path
227 + for _, opt := range cmd.Options {
228 + valopt, ok := cmd.Helptext.SynopsisOptionsValues[opt.Names()[0]]
229 + if !ok {
230 + valopt = opt.Names()[0]
231 + }
232 + sopt := ""
233 + for i, n := range opt.Names() {
234 + pre := "-"
235 + if len(n) > 1 {
236 + pre = "--"
237 + }
238 + if i == 0 {
239 + sopt = fmt.Sprintf("%s%s=<%s>", pre, n, valopt)
240 + } else {
241 + sopt = fmt.Sprintf("%s | %s%s", sopt, pre, n)
242 + }
243 + }
244 + res = fmt.Sprintf("%s [%s]", res, sopt)
245 + }
246 + if len(cmd.Arguments) > 0 {
247 + res = fmt.Sprintf("%s [--]", res)
248 + }
249 + for _, arg := range cmd.Arguments {
250 + sarg := fmt.Sprintf("<%s>", arg.Name)
251 + if arg.Variadic {
252 + sarg = sarg + "..."
253 + }
254 +
255 + if !arg.Required {
256 + sarg = fmt.Sprintf("[%s]", sarg)
257 + }
258 + res = fmt.Sprintf("%s %s", res, sarg)
259 + }
260 + return strings.Trim(res, " ")
261 +}
262 +
263 func argumentText(cmd *cmds.Command) []string {
264 lines := make([]string, len(cmd.Arguments))
265
commands/command.go
+4 -3
@@ -36,9 +36,9 @@ type MarshalerMap map[EncodingType]Marshaler
36 // text follows formats similar to man pages, but not exactly the same.
37 type HelpText struct {
38 // required
39 - Tagline string // used in <cmd usage>
40 - ShortDescription string // used in DESCRIPTION
41 - Synopsis string // showcasing the cmd
39 + Tagline string // used in <cmd usage>
40 + ShortDescription string // used in DESCRIPTION
41 + SynopsisOptionsValues map[string]string // mappings for synopsis generator
42
43 // optional - whole section overrides
44 Usage string // overrides USAGE section
@@ -46,6 +46,7 @@ type HelpText struct {
46 Options string // overrides OPTIONS section
47 Arguments string // overrides ARGUMENTS section
48 Subcommands string // overrides SUBCOMMANDS section
49 + Synopsis string // overrides SYNOPSIS field
50 }
51
52 // Command is a runnable command, with input arguments and options (flags).
test/sharness/t0010-basic-commands.sh
+1 -1
@@ -35,7 +35,7 @@ test_expect_success "ipfs help succeeds" '
35
36 test_expect_success "ipfs help output looks good" '
37 egrep -i "^Usage" help.txt >/dev/null &&
38 - egrep "ipfs .* <command>" help.txt >/dev/null ||
38 + egrep "ipfs <command>" help.txt >/dev/null ||
39 test_fsh cat help.txt
40 '
41
test/sharness/t0040-add-and-cat.sh
+3 -15
@@ -8,19 +8,6 @@ test_description="Test add and cat commands"
8
9 . lib/test-lib.sh
10
11 -client_err_add() {
12 - printf "$@\n\n"
13 - echo 'USAGE
14 - ipfs add <path>... - Add a file to ipfs.
15 -
16 - Adds contents of <path> to ipfs. Use -r to add directories.
17 - Note that directories are added recursively, to form the ipfs
18 - MerkleDAG.
19 -
20 -Use '"'"'ipfs add --help'"'"' for more information about this command.
21 -'
22 -}
23 -
11 test_add_cat_file() {
12 test_expect_success "ipfs add succeeds" '
13 echo "Hello Worlds!" >mountdir/hello.txt &&
@@ -176,9 +163,10 @@ test_add_named_pipe() {
163 test_expect_success "useful error message when adding a named pipe" '
164 mkfifo named-pipe &&
165 test_expect_code 1 ipfs add named-pipe 2>actual &&
179 - client_err_add "Error: Unrecognized file type for named-pipe: $(generic_stat named-pipe)" >expected &&
166 rm named-pipe &&
181 - test_cmp expected actual
167 + grep "Error: Unrecognized file type for named-pipe: $(generic_stat named-pipe)" actual &&
168 + grep USAGE actual &&
169 + grep "ipfs add" actual
170 '
171
172 test_expect_success "useful error message when recursively adding a named pipe" '