-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.go
101 lines (97 loc) · 2.2 KB
/
arguments.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
92
93
94
95
96
97
98
99
100
101
package cmds
import (
"fmt"
"strconv"
"github.com/Clinet/clinet_services"
)
var (
ArgTypeChannel = &services.Channel{}
ArgTypeRole = &services.Role{}
ArgTypeServer = &services.Server{}
ArgTypeUser = &services.User{}
)
type CmdArg struct {
Name string //Display name for argument
Description string //Description for command usage
Value interface{} //Value for argument, set to default value (or zero value if required argument) when creating command
Required bool //True when argument must be changed
}
func NewCmdArg(name, desc string, value interface{}) *CmdArg {
return &CmdArg{
Name: name,
Description: desc,
Value: value,
}
}
func (arg *CmdArg) GetInt() int {
switch arg.Value.(type) {
case string:
num, err := strconv.Atoi(arg.Value.(string))
if err != nil {
return 0
}
return num
case int32:
return int(arg.Value.(int32))
case int64:
return int(arg.Value.(int64))
}
return arg.Value.(int)
}
func (arg *CmdArg) GetInt64() int64 {
switch arg.Value.(type) {
case string:
num, err := strconv.Atoi(arg.Value.(string))
if err != nil {
return 0
}
return int64(num)
case int:
return int64(arg.Value.(int))
case int32:
return int64(arg.Value.(int32))
}
return arg.Value.(int64)
}
func (arg *CmdArg) GetString() string {
switch arg.Value.(type) {
case int:
return fmt.Sprintf("%d", arg.Value.(int))
case int32:
return fmt.Sprintf("%d", arg.Value.(int32))
case int64:
return fmt.Sprintf("%d", arg.Value.(int64))
}
return arg.Value.(string)
}
func (arg *CmdArg) GetBool() bool {
switch arg.Value.(type) {
case bool:
return arg.Value.(bool)
case int:
if arg.Value.(int) > 0 {return true}
case int32:
if arg.Value.(int32) > 0 {return true}
case int64:
if arg.Value.(int64) > 0 {return true}
case string:
switch arg.Value.(string) {
case "t", "T", "true", "True", "TRUE", "y", "Y", "Yes", "YES":
return true
}
}
return false
}
func (arg *CmdArg) GetUser() *services.User {
switch arg.Value.(type) {
case string:
return &services.User{UserID: arg.Value.(string)}
case *services.User:
return arg.Value.(*services.User)
}
return nil
}
func (arg *CmdArg) SetRequired() *CmdArg {
arg.Required = true
return arg
}