Added columns when printing commands.

This commit is contained in:
Andrey Parhomenko 2023-11-11 20:40:53 +03:00
parent f877427ca8
commit 707875fd6a
2 changed files with 47 additions and 39 deletions

View file

@ -10,36 +10,36 @@ import (
var ( var (
root = mtool.T("test").Subs( root = mtool.T("test").Subs(
mtool.T("echo").Func(func(flags *mtool.Flags) { mtool.T("echo").Func(func(flags *mtool.Flags) {
var b bool var b bool
flags.BoolVar(&b, "b", false, "the check flag") flags.BoolVar(&b, "b", false, "the check flag")
args := flags.Parse() args := flags.Parse()
fmt.Println(args) fmt.Println(args)
}).Desc( }).Desc(
"print string array to standard output", "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) {
args := flags.Parse() args := flags.Parse()
one, _ := strconv.Atoi(args[0]) one, _ := strconv.Atoi(args[0])
two, _ := strconv.Atoi(args[1]) two, _ := strconv.Atoi(args[1])
fmt.Println(one + two) fmt.Println(one + two)
}).Desc( }).Desc(
"add one value to another", "add one value to another",
).Usage( ).Usage(
"<int1> <int2>", "<int1> <int2>",
), ),
mtool.T("sub").Subs( mtool.T("sub").Subs(
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(
"first sub tool", "first sub tool",
), ),
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(
"second sub tool", "second sub tool",
), ),
).Desc( ).Desc(
"the tool to demonstrate how subtools work", "the tool to demonstrate how subtools work",
), ),

View file

@ -9,6 +9,8 @@ import (
//path "path/filepath" //path "path/filepath"
"flag" "flag"
"sort" "sort"
"text/tabwriter"
"io"
) )
@ -108,6 +110,23 @@ func (t *Tool) ProgName() string {
return t.name return t.name
} }
func (t *Tool) PrintSubs(out io.Writer) {
w := new(tabwriter.Writer)
w.Init(out, 0, 0, 1, ' ', 0)
defer w.Flush()
keys := make([]string, len(t.subs))
i := 0
for k, _ := range t.subs {
keys[i] = k
i++
}
sort.Strings(keys)
for _, k := range keys {
tool := t.subs[k]
fmt.Fprintf(w, " %s\t%s\n", k, tool.desc)
}
}
func (t *Tool) Run(args []string) { func (t *Tool) Run(args []string) {
var( var(
usageTool *Tool usageTool *Tool
@ -180,13 +199,6 @@ func (t *Tool) Run(args []string) {
// Print available sub commands if // Print available sub commands if
// got no arguments. // got no arguments.
if len(args) == 0 { if len(args) == 0 {
keys := make([]string, len(t.subs))
i := 0
for k, _ := range t.subs {
keys[i] = k
i++
}
sort.Strings(keys)
if t.desc != "" { if t.desc != "" {
fmt.Fprintf( fmt.Fprintf(
@ -202,13 +214,9 @@ func (t *Tool) Run(args []string) {
fmt.Fprintf(out, "\nDescription:\n %s\n", t.ldesc) fmt.Fprintf(out, "\nDescription:\n %s\n", t.ldesc)
} }
if len(keys) > 0 { if len(t.subs) > 0 {
fmt.Fprint(out, "\nCommands:\n") fmt.Fprint(out, "\nCommands:\n")
for _, k := range keys { t.PrintSubs(out)
tool := t.subs[k]
fmt.Fprintf(out, " %s\t%s\n", k, tool.desc)
}
} }
os.Exit(1) os.Exit(1)