-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·91 lines (77 loc) · 2.25 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"log"
_ "net/http/pprof"
commander2 "github.com/pefish/go-commander"
go_config "github.com/pefish/go-config"
)
type Config struct {
Test string `json:"test" default:"default-flag-test" usage:"test flag set"`
FuckInt int `json:"fuck-int" default:"888" usage:"test fuck int"`
Abc int `json:"abc" default:"888" usage:"test fuck int"`
commander2.BasicConfig
}
var GlobalConfig Config
var Data struct {
Data1 string `json:"data1"`
}
type TestSubCommand struct {
}
func (t TestSubCommand) Config() interface{} {
return &GlobalConfig
}
func (t TestSubCommand) Data() interface{} {
return &Data
}
func (t TestSubCommand) Init(command *commander2.Commander) error {
return nil
}
func (t TestSubCommand) Start(command *commander2.Commander) error {
command.Logger.InfoFRaw(
"command: %s, test param: %s, args: %#v",
command.Name,
go_config.ConfigManagerInstance.MustString("test"),
command.Args,
)
command.Logger.InfoFRaw("GlobalConfig: %#v", GlobalConfig)
command.Logger.InfoFRaw("Data: %#v", Data)
Data.Data1 = "data2"
return nil
}
func (t TestSubCommand) OnExited(data *commander2.Commander) error {
//fmt.Println("OnExited")
return nil
}
func main() {
//go_logger.Logger.Error(errors.WithMessage(errors.New("123"), "ywrtsdfhs"))
commander := commander2.NewCommander("test", "v0.0.1", "小工具")
//commander.RegisterSubcommand("test", "这是一个测试", TestSubCommand{})
commander.RegisterSubcommand("test2", &commander2.SubcommandInfo{
Desc: "这是一个 test2 子命令",
Args: []string{
"arg1",
"arg2",
},
Subcommand: TestSubCommand{},
})
commander.RegisterDefaultSubcommand(&commander2.SubcommandInfo{
Desc: "这是默认命令",
Args: []string{
"file",
},
Subcommand: TestSubCommand{},
})
err := commander.Run()
if err != nil {
log.Fatal(err)
}
}
// go run ./_example test2 --version
// go run ./_example --help
// go run ./_example test2 --help
// go run ./_example test2 -- 1.txt 2.txt
// go run ./_example -- 1.txt
// go run ./_example --test="sgdfgs" -- 1.txt
// TEST=env-test go run ./_example --config ./_example/config.yaml -- 1.txt
// FUCK_INT=111 go run ./_example --fuck-int=123 -- 1.txt
// FUCK_INT=111 go run ./_example --fuck-int=123 --env-file=./_example/.env -- 1.txt