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
|
n int
|
||||||
)
|
)
|
||||||
flags.BoolVar(&b, "u", false, "convert to uppercase", "UPPERCASE")
|
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()
|
args := flags.Parse()
|
||||||
|
|
||||||
if b {
|
if b {
|
||||||
|
@ -32,7 +36,10 @@ var (
|
||||||
"print string array to standard output",
|
"print string array to standard output",
|
||||||
).Usage(
|
).Usage(
|
||||||
"[str1 str2 ... strN]",
|
"[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) {
|
mtool.T("sum").Func(func(flags *mtool.Flags) {
|
||||||
args := flags.Parse()
|
args := flags.Parse()
|
||||||
one, _ := strconv.Atoi(args[0])
|
one, _ := strconv.Atoi(args[0])
|
||||||
|
@ -59,8 +66,10 @@ var (
|
||||||
),
|
),
|
||||||
).Desc(
|
).Desc(
|
||||||
"the testing program to show how to use the lib",
|
"the testing program to show how to use the lib",
|
||||||
).Ldesc(`This is the long description where you
|
).Ldesc(`
|
||||||
can put anything you want about the program.`)
|
This is the long description where you
|
||||||
|
can put anything you want about the program.
|
||||||
|
`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -126,12 +126,13 @@ func (t *Tool) Run(args []string) {
|
||||||
nflags := 0
|
nflags := 0
|
||||||
flags.VisitAll(func(f *flag.Flag){
|
flags.VisitAll(func(f *flag.Flag){
|
||||||
nflags++
|
nflags++
|
||||||
|
f.Usage = FormatInCode(f.Usage, false)
|
||||||
varNames, ok := flags.envNameMap[f.Name]
|
varNames, ok := flags.envNameMap[f.Name]
|
||||||
if !ok || len(varNames) == 0 {
|
if !ok || len(varNames) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Usage += " ("
|
f.Usage += "\n("
|
||||||
for i, name := range varNames {
|
for i, name := range varNames {
|
||||||
f.Usage += "$"+name
|
f.Usage += "$"+name
|
||||||
if i < len(varNames) - 1 {
|
if i < len(varNames) - 1 {
|
||||||
|
@ -146,7 +147,7 @@ func (t *Tool) Run(args []string) {
|
||||||
if usageTool.desc != "" {
|
if usageTool.desc != "" {
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
out, "Name:\n %s - %s\n\n",
|
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, "")
|
fmt.Fprintln(out, "")
|
||||||
|
|
||||||
if usageTool.ldesc != "" {
|
if usageTool.ldesc != "" {
|
||||||
fmt.Fprintf(out, "Description:\n %s", usageTool.ldesc)
|
fmt.Fprintf(
|
||||||
|
out,
|
||||||
|
"\nDescription:\n %s\n",
|
||||||
|
FormatInCode(usageTool.ldesc, true),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options
|
// Options
|
||||||
if hasOptions {
|
if hasOptions {
|
||||||
fmt.Fprintln(out, "\nOptions:")
|
fmt.Fprintln(out, "\nOptions:")
|
||||||
fmt.Fprintln(out, " --\n option terminator")
|
fmt.Fprintln(out, " -- option terminator")
|
||||||
flags.PrintDefaults()
|
flags.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,7 +215,7 @@ func (t *Tool) Run(args []string) {
|
||||||
if t.desc != "" {
|
if t.desc != "" {
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
out, "Name:\n %s - %s\n\n",
|
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(
|
fmt.Fprintf(
|
||||||
out,
|
out,
|
||||||
"\nDescription:\n %s\n",
|
"\nDescription:\n %s\n",
|
||||||
t.GetLongFormattedDesc(),
|
FormatInCode(t.ldesc, true),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,7 +256,26 @@ func (t *Tool) Run(args []string) {
|
||||||
sub.Run(args)
|
sub.Run(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tool) GetLongFormattedDesc() string {
|
// Returns the built-in
|
||||||
return strings.ReplaceAll(t.ldesc, "\n", "\n ")
|
// 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