feat: better formatting for the in-code descriptions.
This commit is contained in:
parent
b5baabf74f
commit
141d2a53be
2 changed files with 45 additions and 12 deletions
|
@ -16,7 +16,11 @@ var (
|
|||
n int
|
||||
)
|
||||
flags.BoolVar(&b, "u", false, "convert to uppercase", "UPPERCASE")
|
||||
flags.IntVar(&n, "n", 1, "amount of times to print", "NUMPRINT", "NUMPRINT1")
|
||||
flags.IntVar(&n, "n", 1, `
|
||||
amount of times to print
|
||||
and long long desc to check things
|
||||
`,
|
||||
"NUMPRINT", "NUMPRINT1")
|
||||
args := flags.Parse()
|
||||
|
||||
if b {
|
||||
|
@ -32,7 +36,10 @@ var (
|
|||
"print string array to standard output",
|
||||
).Usage(
|
||||
"[str1 str2 ... strN]",
|
||||
),
|
||||
).Ldesc(`
|
||||
Some long description for the echo program
|
||||
to check how this shit works.
|
||||
`),
|
||||
mtool.T("sum").Func(func(flags *mtool.Flags) {
|
||||
args := flags.Parse()
|
||||
one, _ := strconv.Atoi(args[0])
|
||||
|
@ -59,8 +66,10 @@ var (
|
|||
),
|
||||
).Desc(
|
||||
"the testing program to show how to use the lib",
|
||||
).Ldesc(`This is the long description where you
|
||||
can put anything you want about the program.`)
|
||||
).Ldesc(`
|
||||
This is the long description where you
|
||||
can put anything you want about the program.
|
||||
`)
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -126,12 +126,13 @@ func (t *Tool) Run(args []string) {
|
|||
nflags := 0
|
||||
flags.VisitAll(func(f *flag.Flag){
|
||||
nflags++
|
||||
f.Usage = FormatInCode(f.Usage, false)
|
||||
varNames, ok := flags.envNameMap[f.Name]
|
||||
if !ok || len(varNames) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
f.Usage += " ("
|
||||
f.Usage += "\n("
|
||||
for i, name := range varNames {
|
||||
f.Usage += "$"+name
|
||||
if i < len(varNames) - 1 {
|
||||
|
@ -146,7 +147,7 @@ func (t *Tool) Run(args []string) {
|
|||
if usageTool.desc != "" {
|
||||
fmt.Fprintf(
|
||||
out, "Name:\n %s - %s\n\n",
|
||||
usageTool.FullName(), t.desc,
|
||||
usageTool.FullName(), FormatInCode(t.desc, true),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -171,13 +172,17 @@ func (t *Tool) Run(args []string) {
|
|||
fmt.Fprintln(out, "")
|
||||
|
||||
if usageTool.ldesc != "" {
|
||||
fmt.Fprintf(out, "Description:\n %s", usageTool.ldesc)
|
||||
fmt.Fprintf(
|
||||
out,
|
||||
"\nDescription:\n %s\n",
|
||||
FormatInCode(usageTool.ldesc, true),
|
||||
)
|
||||
}
|
||||
|
||||
// Options
|
||||
if hasOptions {
|
||||
fmt.Fprintln(out, "\nOptions:")
|
||||
fmt.Fprintln(out, " --\n option terminator")
|
||||
fmt.Fprintln(out, " -- option terminator")
|
||||
flags.PrintDefaults()
|
||||
}
|
||||
|
||||
|
@ -210,7 +215,7 @@ func (t *Tool) Run(args []string) {
|
|||
if t.desc != "" {
|
||||
fmt.Fprintf(
|
||||
out, "Name:\n %s - %s\n\n",
|
||||
t.FullName(), t.desc,
|
||||
t.FullName(), FormatInCode(t.desc, true),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -221,7 +226,7 @@ func (t *Tool) Run(args []string) {
|
|||
fmt.Fprintf(
|
||||
out,
|
||||
"\nDescription:\n %s\n",
|
||||
t.GetLongFormattedDesc(),
|
||||
FormatInCode(t.ldesc, true),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -251,7 +256,26 @@ func (t *Tool) Run(args []string) {
|
|||
sub.Run(args)
|
||||
}
|
||||
|
||||
func (t *Tool) GetLongFormattedDesc() string {
|
||||
return strings.ReplaceAll(t.ldesc, "\n", "\n ")
|
||||
// Returns the built-in
|
||||
// string in a more printable format.
|
||||
// Is used in printing descriptions.
|
||||
func FormatInCode(
|
||||
desc string,
|
||||
addSpaces bool,
|
||||
) string {
|
||||
if desc == "" {
|
||||
return desc
|
||||
}
|
||||
desc = strings.ReplaceAll(desc, "\t", "")
|
||||
if desc[0] == '\n' {
|
||||
desc = desc[1:]
|
||||
}
|
||||
if desc[len(desc)-1] == '\n' {
|
||||
desc = desc[:len(desc)-1]
|
||||
}
|
||||
if addSpaces {
|
||||
desc = strings.ReplaceAll(desc, "\n", "\n ")
|
||||
}
|
||||
return desc
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue