-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
67 lines (59 loc) · 1.39 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cmd
import (
"errors"
"fmt"
)
type app struct {
Name string
Version string
Desc string
commands map[string]Command
}
type Command struct {
Name string
Desc string
Excute func(Command) error
Flags map[string]Flag
}
type Flag struct {
Name string
value string
Usage string
Excute func(Command) error
}
func (this *app) AddCommand(command Command) {
if this.commands == nil {
this.commands = map[string]Command{}
}
if command.Flags == nil {
command.Flags = map[string]Flag{}
}
this.commands[command.Name] = command
}
var args = getArgs()
func (this app) Excute() error {
if args.Command == "" {
if command, ok := this.commands["help"]; ok {
return command.Excute(command)
} else {
return errors.New(fmt.Sprint(this.Name, " has not set help command or default help command,if you want a default help,you can set cmd.New(cmd.App{DefaultHelp:true}) to enable it or use addCommand to custom a help command"))
}
}
if command, ok := this.commands[args.Command]; ok {
for k, v := range args.Flags {
if fl, ok := command.Flags[k]; ok {
fl.value = v
command.Flags[k] = fl
if fl.Excute != nil {
return fl.Excute(command)
}
}
}
return command.Excute(command)
} else {
return errors.New(fmt.Sprint("command ", args.Command, " not found,run ", this.Name, " help to get more infomention"))
}
}
func New() *app {
return &app{}
}