2022-06-16 09:02:24 +03:00
|
|
|
package multitool
|
|
|
|
|
|
|
|
import(
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2022-10-11 15:38:48 +03:00
|
|
|
type Tool struct {
|
|
|
|
Handler func(args []string)
|
|
|
|
Desc string
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:02:24 +03:00
|
|
|
|
2022-10-11 15:38:48 +03:00
|
|
|
type Tools map[string] Tool
|
2022-06-16 09:02:24 +03:00
|
|
|
|
|
|
|
func Main(name string, m Tools) {
|
|
|
|
var(
|
|
|
|
utilName string
|
|
|
|
args []string
|
|
|
|
)
|
2022-11-23 10:16:19 +03:00
|
|
|
binBase := path.Base(os.Args[0]) ;
|
|
|
|
binBase = binBase[:len(binBase)-len(path.Ext(binBase))]
|
|
|
|
if binBase != name {
|
2022-06-16 09:02:24 +03:00
|
|
|
utilName = binBase
|
|
|
|
args = os.Args[:]
|
|
|
|
} else {
|
|
|
|
if len(os.Args)<2 {
|
2022-10-11 15:38:48 +03:00
|
|
|
for k, v := range m {
|
|
|
|
fmt.Printf("%s:\t%s\n", k, v.Desc)
|
2022-06-16 09:02:24 +03:00
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
utilName = os.Args[1]
|
|
|
|
args = os.Args[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := m[utilName] ; !ok {
|
|
|
|
fmt.Printf("%s: No such uitl as '%s'.\n", os.Args[0], utilName )
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:38:48 +03:00
|
|
|
m[utilName].Handler(args)
|
2022-06-16 09:02:24 +03:00
|
|
|
}
|
|
|
|
|