Added columns when printing commands.
This commit is contained in:
parent
f877427ca8
commit
707875fd6a
2 changed files with 47 additions and 39 deletions
|
@ -10,36 +10,36 @@ import (
|
|||
var (
|
||||
root = mtool.T("test").Subs(
|
||||
mtool.T("echo").Func(func(flags *mtool.Flags) {
|
||||
var b bool
|
||||
flags.BoolVar(&b, "b", false, "the check flag")
|
||||
args := flags.Parse()
|
||||
fmt.Println(args)
|
||||
}).Desc(
|
||||
"print string array to standard output",
|
||||
).Usage(
|
||||
"[str1 str2 ... strN]",
|
||||
),
|
||||
var b bool
|
||||
flags.BoolVar(&b, "b", false, "the check flag")
|
||||
args := flags.Parse()
|
||||
fmt.Println(args)
|
||||
}).Desc(
|
||||
"print string array to standard output",
|
||||
).Usage(
|
||||
"[str1 str2 ... strN]",
|
||||
),
|
||||
mtool.T("sum").Func(func(flags *mtool.Flags) {
|
||||
args := flags.Parse()
|
||||
one, _ := strconv.Atoi(args[0])
|
||||
two, _ := strconv.Atoi(args[1])
|
||||
fmt.Println(one + two)
|
||||
}).Desc(
|
||||
"add one value to another",
|
||||
).Usage(
|
||||
"<int1> <int2>",
|
||||
),
|
||||
args := flags.Parse()
|
||||
one, _ := strconv.Atoi(args[0])
|
||||
two, _ := strconv.Atoi(args[1])
|
||||
fmt.Println(one + two)
|
||||
}).Desc(
|
||||
"add one value to another",
|
||||
).Usage(
|
||||
"<int1> <int2>",
|
||||
),
|
||||
mtool.T("sub").Subs(
|
||||
mtool.T("first").Func(func(flags *mtool.Flags) {
|
||||
fmt.Println("called the first", flags.Parse())
|
||||
}).Desc(
|
||||
"first sub tool",
|
||||
),
|
||||
fmt.Println("called the first", flags.Parse())
|
||||
}).Desc(
|
||||
"first sub tool",
|
||||
),
|
||||
mtool.T("second").Func(func(flags *mtool.Flags){
|
||||
fmt.Println("called the second", flags.Parse())
|
||||
}).Desc(
|
||||
"second sub tool",
|
||||
),
|
||||
fmt.Println("called the second", flags.Parse())
|
||||
}).Desc(
|
||||
"second sub tool",
|
||||
),
|
||||
).Desc(
|
||||
"the tool to demonstrate how subtools work",
|
||||
),
|
||||
|
|
|
@ -9,6 +9,8 @@ import (
|
|||
//path "path/filepath"
|
||||
"flag"
|
||||
"sort"
|
||||
"text/tabwriter"
|
||||
"io"
|
||||
)
|
||||
|
||||
|
||||
|
@ -108,6 +110,23 @@ func (t *Tool) ProgName() string {
|
|||
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) {
|
||||
var(
|
||||
usageTool *Tool
|
||||
|
@ -180,13 +199,6 @@ func (t *Tool) Run(args []string) {
|
|||
// Print available sub commands if
|
||||
// got no arguments.
|
||||
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 != "" {
|
||||
fmt.Fprintf(
|
||||
|
@ -202,13 +214,9 @@ func (t *Tool) Run(args []string) {
|
|||
fmt.Fprintf(out, "\nDescription:\n %s\n", t.ldesc)
|
||||
}
|
||||
|
||||
if len(keys) > 0 {
|
||||
if len(t.subs) > 0 {
|
||||
fmt.Fprint(out, "\nCommands:\n")
|
||||
for _, k := range keys {
|
||||
|
||||
tool := t.subs[k]
|
||||
fmt.Fprintf(out, " %s\t%s\n", k, tool.desc)
|
||||
}
|
||||
t.PrintSubs(out)
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
|
|
Loading…
Reference in a new issue