More unified argument to the tool processing function.
This commit is contained in:
parent
2fb327b6a6
commit
c382bc403f
2 changed files with 25 additions and 8 deletions
|
@ -9,18 +9,22 @@ import(
|
||||||
var(
|
var(
|
||||||
tools = mtool.Tools{
|
tools = mtool.Tools{
|
||||||
"echo" : mtool.Tool{
|
"echo" : mtool.Tool{
|
||||||
func(args []string, flags *mtool.Flags) {
|
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")
|
||||||
flags.Parse(args)
|
flags.Parse()
|
||||||
fmt.Println(flags.Args())
|
|
||||||
|
args := flags.Args()
|
||||||
|
|
||||||
|
fmt.Println(args)
|
||||||
},
|
},
|
||||||
"print string to standard output string",
|
"print string to standard output string",
|
||||||
"[str1 str2 ... strN]",
|
"[str1 str2 ... strN]",
|
||||||
},
|
},
|
||||||
"sum" : mtool.Tool{
|
"sum" : mtool.Tool{
|
||||||
func(args []string, flags *mtool.Flags) {
|
func(flags *mtool.Flags) {
|
||||||
flags.Parse(args)
|
flags.Parse()
|
||||||
|
args := flags.Args()
|
||||||
one, _ := strconv.Atoi(args[1])
|
one, _ := strconv.Atoi(args[1])
|
||||||
two, _ := strconv.Atoi(args[2])
|
two, _ := strconv.Atoi(args[2])
|
||||||
fmt.Println(one + two)
|
fmt.Println(one + two)
|
||||||
|
|
|
@ -10,9 +10,11 @@ import(
|
||||||
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
|
args []string
|
||||||
|
parsedArgs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handler func(args []string, flags *Flags)
|
type Handler func(*Flags)
|
||||||
|
|
||||||
type Tool struct {
|
type Tool struct {
|
||||||
Handler Handler
|
Handler Handler
|
||||||
|
@ -21,6 +23,15 @@ type Tool struct {
|
||||||
|
|
||||||
type Tools map[string] Tool
|
type Tools map[string] Tool
|
||||||
|
|
||||||
|
func (flags *Flags) Parse() {
|
||||||
|
flags.FlagSet.Parse(flags.args)
|
||||||
|
flags.parsedArgs = flags.FlagSet.Args()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (flags *Flags) Args() []string {
|
||||||
|
return flags.parsedArgs
|
||||||
|
}
|
||||||
|
|
||||||
func Main(name string, m Tools) {
|
func Main(name string, m Tools) {
|
||||||
var(
|
var(
|
||||||
utilName string
|
utilName string
|
||||||
|
@ -65,6 +76,8 @@ func Main(name string, m Tools) {
|
||||||
flagSet := flag.NewFlagSet(arg1, flag.ExitOnError)
|
flagSet := flag.NewFlagSet(arg1, flag.ExitOnError)
|
||||||
flags := &Flags{
|
flags := &Flags{
|
||||||
flagSet,
|
flagSet,
|
||||||
|
[]string{},
|
||||||
|
[]string{},
|
||||||
}
|
}
|
||||||
flags.Usage = func() {
|
flags.Usage = func() {
|
||||||
out := flags.Output()
|
out := flags.Output()
|
||||||
|
@ -102,8 +115,8 @@ func Main(name string, m Tools) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
args = args[1:]
|
flags.args = args[1:]
|
||||||
|
|
||||||
util.Handler(args, flags)
|
util.Handler(flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue