Skip to content

Commit de770e4

Browse files
authored
make goreportcard happy (#5)
Change-Id: Ica8c1aa65a7735dff26b0a27ca21a8c85c218500
1 parent 212ac16 commit de770e4

File tree

5 files changed

+391
-269
lines changed

5 files changed

+391
-269
lines changed

cli.go

Lines changed: 1 addition & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -290,136 +290,7 @@ func (p *App) printSuggestions(invalidCmdName string) {
290290
}
291291

292292
func (p *App) printUsage() {
293-
globalFlags := p.getGlobalFlags()
294-
ctx := p.getParsingContext()
295-
296-
fs := ctx.getFlagSet()
297-
out := getFlagSetOutput(fs)
298-
299-
if ctx.opts.customUsage != nil {
300-
help := strings.TrimSpace(ctx.opts.customUsage())
301-
fmt.Fprintf(out, "%s\n\n", help)
302-
return
303-
}
304-
305-
if !ctx.parsed && globalFlags != nil {
306-
wrapArgs := &withGlobalFlagArgs{
307-
GlobalFlags: globalFlags,
308-
}
309-
err := ctx.parseTags(reflect.ValueOf(wrapArgs).Elem())
310-
if err != nil {
311-
return
312-
}
313-
}
314-
315-
cmds := p.cmds
316-
keepCmdOrder := p.opts.KeepCommandOrder
317-
318-
cmdName := ctx.name
319-
flags := ctx.flags
320-
nonflags := ctx.nonflags
321-
showHidden := ctx.showHidden
322-
323-
flagCount := 0
324-
hasShortFlag := false
325-
for _, f := range flags {
326-
if !f.hidden || showHidden {
327-
flagCount++
328-
hasShortFlag = hasShortFlag || f.short != ""
329-
}
330-
}
331-
subCmds := cmds.listSubCommandsToPrint(cmdName, showHidden)
332-
progName := getProgramName()
333-
hasFlags, hasNonflags := flagCount > 0, len(nonflags) > 0
334-
hasSubCmds := len(subCmds) > 0
335-
336-
usage := ""
337-
cmd := ctx.cmd
338-
if cmd != nil {
339-
if cmd.AliasOf != "" {
340-
usage += cmd.Description + "\n"
341-
cmd = p.cmdMap[cmd.AliasOf]
342-
cmdName = cmd.Name
343-
}
344-
if cmd.Description != "" {
345-
usage += cmd.Description + "\n"
346-
}
347-
if usage != "" {
348-
usage += "\n"
349-
}
350-
}
351-
usage += "USAGE:\n " + progName
352-
if cmdName != "" {
353-
usage += " " + cmdName
354-
}
355-
if hasFlags {
356-
usage += " [flags]"
357-
}
358-
if hasNonflags {
359-
for _, f := range nonflags {
360-
name := f.name
361-
if f.isSlice() {
362-
name += "..."
363-
} else if f.isMap() {
364-
name += "{...}"
365-
}
366-
if f.required {
367-
usage += fmt.Sprintf(" <%s>", name)
368-
} else {
369-
usage += fmt.Sprintf(" [%s]", name)
370-
}
371-
}
372-
}
373-
if !hasFlags && !hasNonflags && hasSubCmds {
374-
usage += " <command> ..."
375-
}
376-
fmt.Fprint(out, usage, "\n\n")
377-
378-
if hasSubCmds {
379-
printSubCommands(out, subCmds, showHidden, keepCmdOrder)
380-
fmt.Fprint(out, "\n")
381-
}
382-
383-
var globalFlagHelp [][2]string
384-
var cmdFlagHelp [][2]string
385-
if flagCount > 0 {
386-
for _, f := range flags {
387-
if f.hidden && !showHidden {
388-
continue
389-
}
390-
name, usage := f.getUsage(hasShortFlag)
391-
if f.isGlobal {
392-
globalFlagHelp = append(globalFlagHelp, [2]string{name, usage})
393-
} else {
394-
cmdFlagHelp = append(cmdFlagHelp, [2]string{name, usage})
395-
}
396-
}
397-
}
398-
if len(cmdFlagHelp) > 0 {
399-
fmt.Fprint(out, "FLAGS:\n")
400-
printWithAlignment(out, cmdFlagHelp)
401-
fmt.Fprint(out, "\n")
402-
}
403-
if len(nonflags) > 0 {
404-
var nonflagLines [][2]string
405-
for _, f := range nonflags {
406-
name, usage := f.getUsage(false)
407-
nonflagLines = append(nonflagLines, [2]string{name, usage})
408-
}
409-
fmt.Fprint(out, "ARGUMENTS:\n")
410-
printWithAlignment(out, nonflagLines)
411-
fmt.Fprint(out, "\n")
412-
}
413-
if len(globalFlagHelp) > 0 {
414-
fmt.Fprint(out, "GLOBAL FLAGS:\n")
415-
printWithAlignment(out, globalFlagHelp)
416-
fmt.Fprint(out, "\n")
417-
}
418-
419-
if ctx.opts.helpFooter != nil {
420-
footer := strings.TrimSpace(ctx.opts.helpFooter())
421-
fmt.Fprintf(out, "%s\n\n", footer)
422-
}
293+
newUsagePrinter(p).Do()
423294
}
424295

425296
func printSubCommands(out io.Writer, cmds commands, showHidden bool, keepCmdOrder bool) {

cli_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ func TestParsing_TagSyntax(t *testing.T) {
286286
M2 string `cli:"#H, --m2 modifier 2"` // hidden
287287
M3 string `cli:"#D, --m3"` // deprecated
288288

289-
// comma seperated
290-
A int `cli:"-a, -a-flag description can be seperated by spaces"`
291-
B int `cli:"-b, --b-flag description can be seperated by spaces"`
289+
// comma separated
290+
A int `cli:"-a, -a-flag description can be separated by spaces"`
291+
B int `cli:"-b, --b-flag description can be separated by spaces"`
292292
C int `cli:"#D, -c, --c-flag, description of 'DVALUE' flag"`
293293

294294
SomeCommonFlags

0 commit comments

Comments
 (0)