Added long descriptions for mtool.

This commit is contained in:
Andrey Parhomenko 2023-11-10 12:10:16 +03:00
parent 6f7bea24d0
commit f877427ca8
2 changed files with 59 additions and 26 deletions

View file

@ -15,15 +15,14 @@ var (
args := flags.Parse() args := flags.Parse()
fmt.Println(args) fmt.Println(args)
}).Desc( }).Desc(
"print string to standard output string", "print string array to standard output",
).Usage( ).Usage(
"[str1 str2 ... strN]", "[str1 str2 ... strN]",
), ),
mtool.T("sum").Func(func(flags *mtool.Flags) { mtool.T("sum").Func(func(flags *mtool.Flags) {
flags.Parse() args := flags.Parse()
args := flags.Args() one, _ := strconv.Atoi(args[0])
one, _ := strconv.Atoi(args[1]) two, _ := strconv.Atoi(args[1])
two, _ := strconv.Atoi(args[2])
fmt.Println(one + two) fmt.Println(one + two)
}).Desc( }).Desc(
"add one value to another", "add one value to another",
@ -34,18 +33,21 @@ var (
mtool.T("first").Func(func(flags *mtool.Flags) { mtool.T("first").Func(func(flags *mtool.Flags) {
fmt.Println("called the first", flags.Parse()) fmt.Println("called the first", flags.Parse())
}).Desc( }).Desc(
"description", "first sub tool",
).Usage(
"[nothing here]",
), ),
mtool.T("second").Func(func(flags *mtool.Flags){ mtool.T("second").Func(func(flags *mtool.Flags){
fmt.Println("called the second", flags.Parse()) fmt.Println("called the second", flags.Parse())
}).Desc( }).Desc(
"description", "second sub tool",
).Usage(
"[nothing here]",
), ),
), ).Desc(
"the tool to demonstrate how subtools work",
),
).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",
) )
) )

View file

@ -31,7 +31,7 @@ func (fn HandlerFunc) Handle(flags *Flags) {
type Tool struct { type Tool struct {
name string name string
handler Handler handler Handler
desc, usage string desc, ldesc, usage string
subs ToolMap subs ToolMap
parent *Tool parent *Tool
} }
@ -58,6 +58,11 @@ func (t *Tool) Desc(d string) *Tool {
return t return t
} }
func (t *Tool) Ldesc(d string) *Tool {
t.ldesc = d
return t
}
func (t *Tool) Usage(u string) *Tool { func (t *Tool) Usage(u string) *Tool {
t.usage = u t.usage = u
return t return t
@ -92,6 +97,10 @@ func (t *Tool) FullName() string {
return ret return ret
} }
func (t *Tool) IsRoot() bool {
return t.parent == nil
}
func (t *Tool) ProgName() string { func (t *Tool) ProgName() string {
for t.parent != nil { for t.parent != nil {
t = t.parent t = t.parent
@ -114,15 +123,21 @@ func (t *Tool) Run(args []string) {
out := flags.Output() out := flags.Output()
flags.Usage = func() { flags.Usage = func() {
n := 0 n := 0
// We can visit the flags since the
// function will be called after
// parsing.
flags.VisitAll(func(f *flag.Flag){ flags.VisitAll(func(f *flag.Flag){
n++ n++
}) })
hasOptions := n != 0 hasOptions := n != 0
// Name
if usageTool.desc != "" {
fmt.Fprintf(
out, "Name:\n %s - %s\n\n",
usageTool.FullName(), t.desc,
)
}
// Usage
fmt.Fprintf( fmt.Fprintf(
out, "Usage:\n %s", out, "Usage:\n %s",
usageTool.FullName(), usageTool.FullName(),
@ -140,13 +155,17 @@ func (t *Tool) Run(args []string) {
} }
fmt.Fprintln(out, "") fmt.Fprintln(out, "")
if usageTool.ldesc != "" {
fmt.Fprintf(out, "Description:\n %s", usageTool.ldesc)
}
// Options
if hasOptions { if hasOptions {
fmt.Fprintln(out, "\nOptions:") fmt.Fprintln(out, "\nOptions:")
flags.PrintDefaults() flags.PrintDefaults()
} }
os.Exit(1)
} }
flags.args = args flags.args = args
@ -168,15 +187,27 @@ func (t *Tool) Run(args []string) {
i++ i++
} }
sort.Strings(keys) sort.Strings(keys)
if t.desc != "" {
fmt.Fprintf(
out, "Name:\n %s - %s\n\n",
t.FullName(), t.desc,
)
}
fmt.Fprintf(out, "Usage:\n"+ fmt.Fprintf(out, "Usage:\n"+
" %s <command> [options] [arguments]\n\n" + " %s <command>\n", t.FullName())
"Commands:\n", t.FullName())
for i, k := range keys { if t.ldesc != "" {
tool := t.subs[k] fmt.Fprintf(out, "\nDescription:\n %s\n", t.ldesc)
fmt.Fprintf(out, " %s\t%s\n", k, tool.desc) }
if i != len(keys) - 1 {
fmt.Fprintln(out, "") if len(keys) > 0 {
fmt.Fprint(out, "\nCommands:\n")
for _, k := range keys {
tool := t.subs[k]
fmt.Fprintf(out, " %s\t%s\n", k, tool.desc)
} }
} }