improve help content printing for package gcmd

This commit is contained in:
John Guo
2021-12-16 12:45:00 +08:00
parent 19c9f0a488
commit f4e8eff813
2 changed files with 83 additions and 41 deletions

View File

@ -65,21 +65,18 @@ func (c *Command) Print() {
}
}
for _, cmd := range c.commands {
brief := gstr.Replace(cmd.Brief, "\n", "")
// Add "..." to brief for those commands that also have sub-commands.
if len(cmd.commands) > 0 {
brief = gstr.TrimRight(brief, ".") + "..."
}
var (
spaceLength = maxSpaceLength - len(cmd.Name)
wordwrapPrefix = gstr.Repeat(" ", len(prefix+cmd.Name)+spaceLength+4)
lineStr = fmt.Sprintf(
"%s%s%s%s\n",
prefix, cmd.Name, gstr.Repeat(" ", spaceLength+4), gstr.Trim(brief),
)
)
lineStr = gstr.WordWrap(lineStr, maxLineChars, "\n"+wordwrapPrefix)
buffer.WriteString(lineStr)
c.printLineBrief(printLineBriefInput{
Buffer: buffer,
Name: cmd.Name,
Prefix: prefix,
Brief: gstr.Trim(cmd.Brief),
WordwrapPrefix: wordwrapPrefix,
SpaceLength: spaceLength,
})
}
buffer.WriteString("\n")
}
@ -103,16 +100,17 @@ func (c *Command) Print() {
continue
}
var (
brief = gstr.Trim(gstr.Replace(arg.Brief, "\n", ""))
spaceLength = maxSpaceLength - len(arg.Name)
wordwrapPrefix = gstr.Repeat(" ", len(prefix+arg.Name)+spaceLength+4)
lineStr = fmt.Sprintf(
"%s%s%s%s\n",
prefix, arg.Name, gstr.Repeat(" ", spaceLength+4), brief,
)
)
lineStr = gstr.WordWrap(lineStr, maxLineChars, "\n"+wordwrapPrefix)
buffer.WriteString(lineStr)
c.printLineBrief(printLineBriefInput{
Buffer: buffer,
Name: arg.Name,
Prefix: prefix,
Brief: gstr.Trim(arg.Brief),
WordwrapPrefix: wordwrapPrefix,
SpaceLength: spaceLength,
})
}
buffer.WriteString("\n")
}
@ -147,16 +145,18 @@ func (c *Command) Print() {
nameStr = fmt.Sprintf("-/--%s", arg.Name)
}
var (
brief = gstr.Trim(gstr.Replace(arg.Brief, "\n", ""))
brief = gstr.Trim(arg.Brief)
spaceLength = maxSpaceLength - len(nameStr)
wordwrapPrefix = gstr.Repeat(" ", len(prefix+nameStr)+spaceLength+4)
lineStr = fmt.Sprintf(
"%s%s%s%s\n",
prefix, nameStr, gstr.Repeat(" ", spaceLength+4), brief,
)
)
lineStr = gstr.WordWrap(lineStr, maxLineChars, "\n"+wordwrapPrefix)
buffer.WriteString(lineStr)
c.printLineBrief(printLineBriefInput{
Buffer: buffer,
Name: nameStr,
Prefix: prefix,
Brief: brief,
WordwrapPrefix: wordwrapPrefix,
SpaceLength: spaceLength,
})
}
buffer.WriteString("\n")
}
@ -193,6 +193,34 @@ func (c *Command) Print() {
fmt.Println(buffer.String())
}
type printLineBriefInput struct {
Buffer *bytes.Buffer
Name string
Prefix string
Brief string
WordwrapPrefix string
SpaceLength int
}
func (c *Command) printLineBrief(in printLineBriefInput) {
for i, line := range gstr.SplitAndTrim(in.Brief, "\n") {
var lineStr string
if i == 0 {
lineStr = fmt.Sprintf(
"%s%s%s%s\n",
in.Prefix, in.Name, gstr.Repeat(" ", in.SpaceLength+4), line,
)
} else {
lineStr = fmt.Sprintf(
"%s%s%s%s\n",
in.Prefix, gstr.Repeat(" ", len(in.Name)), gstr.Repeat(" ", in.SpaceLength+4), line,
)
}
lineStr = gstr.WordWrap(lineStr, maxLineChars, "\n"+in.WordwrapPrefix)
in.Buffer.WriteString(lineStr)
}
}
func (c *Command) defaultHelpFunc(ctx context.Context, parser *Parser) error {
// Print command help info to stdout.
c.Print()

View File

@ -91,31 +91,45 @@ PLATFORMS
destination file path for packed file. if extension of the filename is ".go" and "-n" option is given,
it enables packing SRC to go file, or else it packs SRC into a binary file.
`
commandGenDaoBriefJsonCase = `
generated json tag case for model struct, cases are as follows:
| Case | Example |
|---------------- |--------------------|
| Camel | AnyKindOfString |
| CamelLower | anyKindOfString | default
| Snake | any_kind_of_string |
| SnakeScreaming | ANY_KIND_OF_STRING |
| SnakeFirstUpper | rgb_code_md5 |
| Kebab | any-kind-of-string |
| KebabScreaming | ANY-KIND-OF-STRING |
`
)
func init() {
gtag.Sets(map[string]string{
`commandBuildBrief`: commandBuildBrief,
`commandBuildDc`: commandBuildDc,
`commandBuildEg`: commandBuildEg,
`commandBuildAd`: commandBuildAd,
`commandBuildBriefPack`: commandBuildBriefPack,
`commandBuildBrief`: commandBuildBrief,
`commandBuildDc`: commandBuildDc,
`commandBuildEg`: commandBuildEg,
`commandBuildAd`: commandBuildAd,
`commandBuildBriefPack`: commandBuildBriefPack,
`commandGenDaoBriefJsonCase`: commandGenDaoBriefJsonCase,
})
}
type commandBuildInput struct {
g.Meta `name:"build" config:"gfcli.build"`
Name string `short:"n" name:"name" brief:"output binary name"`
Version string `short:"v" name:"version" brief:"output binary version"`
Arch string `short:"a" name:"arch" brief:"output binary architecture, multiple arch separated with ','"`
System string `short:"s" name:"system" brief:"output binary system, multiple os separated with ','"`
Output string `short:"o" name:"output" brief:"output binary path, used when building single binary file"`
Path string `short:"p" name:"path" brief:"output binary directory path, default is './bin'" d:"./bin"`
Extra string `short:"e" name:"extra" brief:"extra custom \"go build\" options"`
Mod string `short:"m" name:"mod" brief:"like \"-mod\" option of \"go build\", use \"-m none\" to disable go module"`
Cgo bool `short:"c" name:"cgo" brief:"enable or disable cgo feature, it's disabled in default" orphan:"true"`
Pack string `name:"pack" brief:"{commandBuildBriefPack}"`
g.Meta `name:"build" config:"gfcli.build"`
Name string `short:"n" name:"name" brief:"output binary name"`
Version string `short:"v" name:"version" brief:"output binary version"`
Arch string `short:"a" name:"arch" brief:"output binary architecture, multiple arch separated with ','"`
System string `short:"s" name:"system" brief:"output binary system, multiple os separated with ','"`
Output string `short:"o" name:"output" brief:"output binary path, used when building single binary file"`
Path string `short:"p" name:"path" brief:"output binary directory path, default is './bin'" d:"./bin"`
Extra string `short:"e" name:"extra" brief:"extra custom \"go build\" options"`
Mod string `short:"m" name:"mod" brief:"like \"-mod\" option of \"go build\", use \"-m none\" to disable go module"`
Cgo bool `short:"c" name:"cgo" brief:"enable or disable cgo feature, it's disabled in default" orphan:"true"`
JsonCase string `short:"j" name:"jsonCase" brief:"{commandGenDaoBriefJsonCase}" d:"CamelLower"`
Pack string `name:"pack" brief:"{commandBuildBriefPack}"`
}
type commandBuildOutput struct{}