From 141d2a53beb96813b8f1cc0b365cd8bcb1a433f3 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 21 Jul 2024 02:47:00 +0500 Subject: [PATCH] feat: better formatting for the in-code descriptions. --- cmd/test/main.go | 17 +++++++++++++---- mtool/tool.go | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/cmd/test/main.go b/cmd/test/main.go index 6675c4a..b49428e 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -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() { diff --git a/mtool/tool.go b/mtool/tool.go index 13657f0..7115bed 100644 --- a/mtool/tool.go +++ b/mtool/tool.go @@ -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 }