This commit is contained in:
k1574 2022-06-16 11:02:24 +05:00
commit 023d1559d7
4 changed files with 71 additions and 0 deletions

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module github.com/k1574/gomtool
go 1.18

25
m/cmd/test/main.go Normal file
View file

@ -0,0 +1,25 @@
package main
import(
"github.com/k1574/gomtool/m/multitool"
"strconv"
"fmt"
)
var(
tools = multitool.Tools{
"echo" : func(args []string) {
fmt.Println(args)
},
"sum" : func(args []string) {
one, _ := strconv.Atoi(args[1])
two, _ := strconv.Atoi(args[2])
fmt.Println(one + two)
},
}
)
func main() {
multitool.Main("test", tools)
}

39
m/multitool/main.go Normal file
View file

@ -0,0 +1,39 @@
package multitool
import(
"fmt"
"os"
"path"
)
type Tools map[string] func(args []string)
func Main(name string, m Tools) {
var(
utilName string
args []string
)
if binBase := path.Base(os.Args[0]) ; binBase != name {
utilName = binBase
args = os.Args[:]
} else {
if len(os.Args)<2 {
for k, _ := range m {
fmt.Printf("%s\n", k)
}
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)
}
m[utilName](args)
}

4
readme Normal file
View file

@ -0,0 +1,4 @@
# gomtool
Go multitool package. Suits the task of multiple programs in one like in "goblin".