2024-04-07 16:34:29 +03:00
|
|
|
package mtool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"text/tabwriter"
|
2024-07-21 00:18:38 +03:00
|
|
|
"strings"
|
2024-04-07 16:34:29 +03:00
|
|
|
"flag"
|
|
|
|
"sort"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Tool struct {
|
|
|
|
name string
|
|
|
|
handler Handler
|
|
|
|
desc, ldesc, usage string
|
|
|
|
subs ToolMap
|
|
|
|
parent *Tool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns new empty tool with specified name.
|
|
|
|
func T(name string) *Tool {
|
|
|
|
ret := &Tool{}
|
|
|
|
ret.name = name
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tool) Handler(h Handler) *Tool {
|
|
|
|
t.handler = h
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tool) Func(fn HandlerFunc) *Tool {
|
|
|
|
t.handler = fn
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tool) Desc(d string) *Tool {
|
|
|
|
t.desc = d
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tool) Subs(tools ...*Tool) *Tool {
|
|
|
|
if t.subs == nil {
|
|
|
|
t.subs = ToolMap{}
|
|
|
|
}
|
|
|
|
for _, tool := range tools {
|
|
|
|
tool.parent = t
|
|
|
|
t.subs[tool.name] = tool
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tool) Name() string {
|
|
|
|
return t.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tool) FullName() string {
|
|
|
|
ret := ""
|
|
|
|
for t != nil {
|
|
|
|
ret = t.name + func() string {
|
|
|
|
if ret != "" {
|
|
|
|
return " "
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}() + ret
|
|
|
|
t = t.parent
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tool) IsRoot() bool {
|
|
|
|
return t.parent == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tool) ProgName() string {
|
|
|
|
for t.parent != nil {
|
|
|
|
t = t.parent
|
|
|
|
}
|
|
|
|
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
|
|
|
|
)
|
|
|
|
// Should implement the shit later
|
|
|
|
//binBase := path.Base(arg0) ;
|
|
|
|
//binBase = binBase[:len(binBase)-len(path.Ext(binBase))]
|
|
|
|
|
|
|
|
flagSet := flag.NewFlagSet(t.FullName(), flag.ExitOnError)
|
|
|
|
flags := &Flags{
|
|
|
|
FlagSet : flagSet,
|
|
|
|
envMap: make(map[string]string),
|
2024-07-21 00:18:38 +03:00
|
|
|
envNameMap: make(map[string] []string),
|
2024-04-07 16:34:29 +03:00
|
|
|
}
|
|
|
|
out := flags.Output()
|
|
|
|
flags.Usage = func() {
|
2024-07-21 00:18:38 +03:00
|
|
|
nflags := 0
|
2024-04-07 16:34:29 +03:00
|
|
|
flags.VisitAll(func(f *flag.Flag){
|
2024-07-21 00:18:38 +03:00
|
|
|
nflags++
|
2024-07-21 00:47:00 +03:00
|
|
|
f.Usage = FormatInCode(f.Usage, false)
|
2024-07-21 00:18:38 +03:00
|
|
|
varNames, ok := flags.envNameMap[f.Name]
|
|
|
|
if !ok || len(varNames) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-21 00:47:00 +03:00
|
|
|
f.Usage += "\n("
|
2024-07-21 00:18:38 +03:00
|
|
|
for i, name := range varNames {
|
|
|
|
f.Usage += "$"+name
|
|
|
|
if i < len(varNames) - 1 {
|
|
|
|
f.Usage += ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.Usage += ")"
|
2024-04-07 16:34:29 +03:00
|
|
|
})
|
2024-07-21 00:18:38 +03:00
|
|
|
hasOptions := nflags != 0
|
2024-04-07 16:34:29 +03:00
|
|
|
|
|
|
|
// Name
|
|
|
|
if usageTool.desc != "" {
|
|
|
|
fmt.Fprintf(
|
|
|
|
out, "Name:\n %s - %s\n\n",
|
2024-07-21 00:47:00 +03:00
|
|
|
usageTool.FullName(), FormatInCode(t.desc, true),
|
2024-04-07 16:34:29 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Usage
|
|
|
|
fmt.Fprintf(
|
|
|
|
out, "Usage:\n %s",
|
|
|
|
usageTool.FullName(),
|
|
|
|
)
|
|
|
|
if hasOptions {
|
|
|
|
fmt.Fprintf(out, " [options]")
|
|
|
|
}
|
|
|
|
|
|
|
|
if usageTool.usage != "" {
|
|
|
|
fmt.Fprintf(
|
|
|
|
out,
|
|
|
|
" %s",
|
|
|
|
usageTool.usage,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(out, "")
|
|
|
|
|
|
|
|
if usageTool.ldesc != "" {
|
2024-07-21 00:47:00 +03:00
|
|
|
fmt.Fprintf(
|
|
|
|
out,
|
|
|
|
"\nDescription:\n %s\n",
|
|
|
|
FormatInCode(usageTool.ldesc, true),
|
|
|
|
)
|
2024-04-07 16:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options
|
|
|
|
if hasOptions {
|
|
|
|
fmt.Fprintln(out, "\nOptions:")
|
2024-07-21 00:47:00 +03:00
|
|
|
fmt.Fprintln(out, " -- option terminator")
|
2024-04-07 16:34:29 +03:00
|
|
|
flags.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.args = args
|
|
|
|
|
|
|
|
// If the tool has its own handler run it.
|
|
|
|
if t.handler != nil {
|
|
|
|
usageTool = t
|
|
|
|
t.handler.Handle(flags)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print available sub commands if
|
|
|
|
// got no arguments.
|
2024-07-21 00:18:38 +03:00
|
|
|
if len(args) == 0 || func() bool {
|
|
|
|
toolName := args[0]
|
|
|
|
_, ok := t.subs[toolName]
|
|
|
|
if ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if toolName != "" && toolName[0]=='-' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}() {
|
|
|
|
|
2024-04-07 16:34:29 +03:00
|
|
|
|
|
|
|
if t.desc != "" {
|
|
|
|
fmt.Fprintf(
|
|
|
|
out, "Name:\n %s - %s\n\n",
|
2024-07-21 00:47:00 +03:00
|
|
|
t.FullName(), FormatInCode(t.desc, true),
|
2024-04-07 16:34:29 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(out, "Usage:\n"+
|
|
|
|
" %s <command>\n", t.FullName())
|
|
|
|
|
|
|
|
if t.ldesc != "" {
|
2024-07-21 00:18:38 +03:00
|
|
|
fmt.Fprintf(
|
|
|
|
out,
|
|
|
|
"\nDescription:\n %s\n",
|
2024-07-21 00:47:00 +03:00
|
|
|
FormatInCode(t.ldesc, true),
|
2024-07-21 00:18:38 +03:00
|
|
|
)
|
2024-04-07 16:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(t.subs) > 0 {
|
|
|
|
fmt.Fprint(out, "\nCommands:\n")
|
|
|
|
t.PrintSubs(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
toolName := args[0]
|
|
|
|
args = args[1:]
|
|
|
|
|
|
|
|
if _, ok := t.subs[toolName] ; !ok {
|
2024-07-21 00:18:38 +03:00
|
|
|
fmt.Fprintf(
|
|
|
|
out,
|
|
|
|
"%s: No such util %q'\n" +
|
|
|
|
"use ' %s ' to see available commands\n",
|
|
|
|
t.FullName(), toolName,
|
|
|
|
t.FullName(),
|
|
|
|
)
|
2024-04-07 16:34:29 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub := t.subs[toolName]
|
|
|
|
usageTool = sub
|
|
|
|
sub.Run(args)
|
|
|
|
}
|
2024-07-21 00:18:38 +03:00
|
|
|
|
2024-07-21 00:47:00 +03:00
|
|
|
// Returns the built-in
|
|
|
|
// 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
|
2024-07-21 00:18:38 +03:00
|
|
|
}
|
|
|
|
|