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(
|
||||
tools = mtool.Tools{
|
||||
"echo" : mtool.Tool{
|
||||
func(args []string, flags *mtool.Flags) {
|
||||
func(flags *mtool.Flags) {
|
||||
var b bool
|
||||
flags.BoolVar(&b, "b", false, "the check flag")
|
||||
flags.Parse(args)
|
||||
fmt.Println(flags.Args())
|
||||
flags.Parse()
|
||||
|
||||
args := flags.Args()
|
||||
|
||||
fmt.Println(args)
|
||||
},
|
||||
"print string to standard output string",
|
||||
"[str1 str2 ... strN]",
|
||||
},
|
||||
"sum" : mtool.Tool{
|
||||
func(args []string, flags *mtool.Flags) {
|
||||
flags.Parse(args)
|
||||
func(flags *mtool.Flags) {
|
||||
flags.Parse()
|
||||
args := flags.Args()
|
||||
one, _ := strconv.Atoi(args[1])
|
||||
two, _ := strconv.Atoi(args[2])
|
||||
fmt.Println(one + two)
|
||||
|
|
|
@ -10,9 +10,11 @@ import(
|
|||
|
||||
type Flags struct {
|
||||
*flag.FlagSet
|
||||
args []string
|
||||
parsedArgs []string
|
||||
}
|
||||
|
||||
type Handler func(args []string, flags *Flags)
|
||||
type Handler func(*Flags)
|
||||
|
||||
type Tool struct {
|
||||
Handler Handler
|
||||
|
@ -21,6 +23,15 @@ type Tool struct {
|
|||
|
||||
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) {
|
||||
var(
|
||||
utilName string
|
||||
|
@ -65,6 +76,8 @@ func Main(name string, m Tools) {
|
|||
flagSet := flag.NewFlagSet(arg1, flag.ExitOnError)
|
||||
flags := &Flags{
|
||||
flagSet,
|
||||
[]string{},
|
||||
[]string{},
|
||||
}
|
||||
flags.Usage = func() {
|
||||
out := flags.Output()
|
||||
|
@ -102,8 +115,8 @@ func Main(name string, m Tools) {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
args = args[1:]
|
||||
flags.args = args[1:]
|
||||
|
||||
util.Handler(args, flags)
|
||||
util.Handler(flags)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue