Small cleanup to cli formatting
This commit is contained in:
parent
10758f11a2
commit
1c1b05db06
@ -36,6 +36,14 @@ type subCmd struct {
|
||||
passthroughArgs bool
|
||||
}
|
||||
|
||||
func (c subCmd) fullName() string {
|
||||
name := c.name
|
||||
if c.plural != "" {
|
||||
name += "(" + c.plural + ")"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
type subCmdCtxOpts struct {
|
||||
args []string // command-line arguments, excluding the subCmd itself.
|
||||
subCmdNames []string // names of subCmds so far, including this one
|
||||
@ -97,7 +105,7 @@ func usagePrefix(subCmdNames []string) string {
|
||||
subCmdNamesStr += " "
|
||||
}
|
||||
|
||||
return fmt.Sprintf("\nUSAGE: %s %s", os.Args[0], subCmdNamesStr)
|
||||
return fmt.Sprintf("USAGE:\n %s %s", os.Args[0], subCmdNamesStr)
|
||||
}
|
||||
|
||||
func (ctx subCmdCtx) getDaemonRPC() daemon.RPC {
|
||||
@ -144,6 +152,12 @@ func (ctx subCmdCtx) withParsedFlags() (subCmdCtx, error) {
|
||||
})
|
||||
|
||||
ctx.flags.Usage = func() {
|
||||
if ctx.subCmd.descr != "" {
|
||||
fmt.Fprintf(
|
||||
os.Stderr, "\nDESCRIPTION:\n %s\n\n", ctx.subCmd.descr,
|
||||
)
|
||||
}
|
||||
|
||||
var passthroughStr string
|
||||
if ctx.subCmd.passthroughArgs {
|
||||
passthroughStr = " [--] [args...]"
|
||||
@ -153,8 +167,9 @@ func (ctx subCmdCtx) withParsedFlags() (subCmdCtx, error) {
|
||||
os.Stderr, "%s[-h|--help] [%s flags...]%s\n\n",
|
||||
usagePrefix(ctx.opts.subCmdNames), ctx.subCmd.name, passthroughStr,
|
||||
)
|
||||
|
||||
fmt.Fprintf(
|
||||
os.Stderr, "%s FLAGS:\n\n", strings.ToUpper(ctx.subCmd.name),
|
||||
os.Stderr, "%s FLAGS:\n", strings.ToUpper(ctx.subCmd.name),
|
||||
)
|
||||
fmt.Fprintln(os.Stderr, ctx.flags.FlagUsages())
|
||||
|
||||
@ -176,22 +191,39 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error {
|
||||
|
||||
printUsageExit := func(subCmdName string) {
|
||||
|
||||
fmt.Fprintf(os.Stderr, "unknown sub-command %q\n", subCmdName)
|
||||
fmt.Fprintf(os.Stderr, "unknown sub-command %q\n\n", subCmdName)
|
||||
|
||||
if ctx.subCmd.descr != "" {
|
||||
fmt.Fprintf(
|
||||
os.Stderr, "DESCRIPTION:\n %s\n\n", ctx.subCmd.descr,
|
||||
)
|
||||
}
|
||||
|
||||
fmt.Fprintf(
|
||||
os.Stderr,
|
||||
"%s<subCmd> [-h|--help] [sub-command flags...]\n",
|
||||
"%s<subCmd> [-h|--help] [sub-command flags...]\n\n",
|
||||
usagePrefix(ctx.opts.subCmdNames),
|
||||
)
|
||||
|
||||
fmt.Fprintf(os.Stderr, "\nSUB-COMMANDS:\n\n")
|
||||
fmt.Fprintf(os.Stderr, "SUB-COMMANDS:\n")
|
||||
|
||||
var maxNameLen int
|
||||
for _, subCmd := range subCmds {
|
||||
l := len(subCmd.fullName())
|
||||
if l > maxNameLen {
|
||||
maxNameLen = l
|
||||
}
|
||||
}
|
||||
|
||||
for _, subCmd := range subCmds {
|
||||
name := subCmd.name
|
||||
if subCmd.plural != "" {
|
||||
name += "(" + subCmd.plural + ")"
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, " %s\t%s\n", name, subCmd.descr)
|
||||
var (
|
||||
name = subCmd.fullName()
|
||||
padding = strings.Repeat(" ", maxNameLen-len(name)+3)
|
||||
)
|
||||
|
||||
fmt.Fprintf(
|
||||
os.Stderr, " %s%s%s\n", name, padding, subCmd.descr,
|
||||
)
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "\n")
|
||||
|
Loading…
Reference in New Issue
Block a user