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()
fmt.Println(args)
}).Desc(
"print string to standard output string",
"print string array to standard output",
).Usage(
"[str1 str2 ... strN]",
),
mtool.T("sum").Func(func(flags *mtool.Flags) {
flags.Parse()
args := flags.Args()
one, _ := strconv.Atoi(args[1])
two, _ := strconv.Atoi(args[2])
args := flags.Parse()
one, _ := strconv.Atoi(args[0])
two, _ := strconv.Atoi(args[1])
fmt.Println(one + two)
}).Desc(
"add one value to another",
@ -34,18 +33,21 @@ var (
mtool.T("first").Func(func(flags *mtool.Flags) {
fmt.Println("called the first", flags.Parse())
}).Desc(
"description",
).Usage(
"[nothing here]",
"first sub tool",
),
mtool.T("second").Func(func(flags *mtool.Flags){
fmt.Println("called the second", flags.Parse())
}).Desc(
"description",
).Usage(
"[nothing here]",
"second sub tool",
),
).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 {
name string
handler Handler
desc, usage string
desc, ldesc, usage string
subs ToolMap
parent *Tool
}
@ -58,6 +58,11 @@ func (t *Tool) Desc(d string) *Tool {
return t
}
func (t *Tool) Ldesc(d string) *Tool {
t.ldesc = d
return t
}
func (t *Tool) Usage(u string) *Tool {
t.usage = u
return t
@ -92,6 +97,10 @@ func (t *Tool) FullName() string {
return ret
}
func (t *Tool) IsRoot() bool {
return t.parent == nil
}
func (t *Tool) ProgName() string {
for t.parent != nil {
t = t.parent
@ -114,15 +123,21 @@ func (t *Tool) Run(args []string) {
out := flags.Output()
flags.Usage = func() {
n := 0
// We can visit the flags since the
// function will be called after
// parsing.
flags.VisitAll(func(f *flag.Flag){
n++
})
hasOptions := n != 0
// Name
if usageTool.desc != "" {
fmt.Fprintf(
out, "Name:\n %s - %s\n\n",
usageTool.FullName(), t.desc,
)
}
// Usage
fmt.Fprintf(
out, "Usage:\n %s",
usageTool.FullName(),
@ -141,12 +156,16 @@ func (t *Tool) Run(args []string) {
fmt.Fprintln(out, "")
if usageTool.ldesc != "" {
fmt.Fprintf(out, "Description:\n %s", usageTool.ldesc)
}
// Options
if hasOptions {
fmt.Fprintln(out, "\nOptions:")
flags.PrintDefaults()
}
os.Exit(1)
}
flags.args = args
@ -169,14 +188,26 @@ func (t *Tool) Run(args []string) {
}
sort.Strings(keys)
if t.desc != "" {
fmt.Fprintf(
out, "Name:\n %s - %s\n\n",
t.FullName(), t.desc,
)
}
fmt.Fprintf(out, "Usage:\n"+
" %s <command> [options] [arguments]\n\n" +
"Commands:\n", t.FullName())
for i, k := range keys {
" %s <command>\n", t.FullName())
if t.ldesc != "" {
fmt.Fprintf(out, "\nDescription:\n %s\n", t.ldesc)
}
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)
if i != len(keys) - 1 {
fmt.Fprintln(out, "")
}
}