-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.v
124 lines (116 loc) · 3.63 KB
/
commands.v
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
module vargus
// NOTE:
// The function below is repetitive,
// I explicitly did not combine them since it leaks pointer
// memory error (indirectly lost) in valgrind.
// new creates a new commander root instance
pub fn new(cmdConfig CmdConfig) &Commander {
// commands created from new are root
return create_command(cmdConfig, true)
}
// add_command adds a command to the main vargus commander instance
pub fn (mut c Commander) add_command(cmdConfig CmdConfig) &Commander {
// commands created from add_command are non-root
cmd := create_command(cmdConfig, false)
// append to parent command's sub_commands
c.sub_commands << cmd
c.sub_commands_string << cmd.command
// return the cmd from the sub_commands
return c.sub_commands[c.sub_commands.index(cmd)]
}
fn create_command(cmdConfig CmdConfig, root bool) &Commander {
// check cmdconfig command
command_checker(cmdConfig)
t := CmdConfig{}
return &Commander{
command: cmdConfig.command
short_desc: cmdConfig.short_desc
long_desc: cmdConfig.long_desc
allow_next_args: cmdConfig.allow_next_args
is_root: root
function: cmdConfig.function
exec_func: if t.function != cmdConfig.function { true } else { false }
hooks: CmdHooks{
pre_run: cmdConfig.hooks.pre_run
use_pre_run: if t.hooks.pre_run != cmdConfig.hooks.pre_run { true } else { false }
post_run: cmdConfig.hooks.post_run
use_post_run: if t.hooks.post_run != cmdConfig.hooks.post_run { true } else { false }
}
persistent_hooks: PersistentCmdHooks{
persistent_pre_run: cmdConfig.hooks.persistent_pre_run
use_persistent_pre_run: if t.hooks.persistent_pre_run != cmdConfig.hooks.persistent_pre_run {
true
} else {
false
}
persistent_post_run: cmdConfig.hooks.persistent_post_run
use_persistent_post_run: if t.hooks.persistent_post_run != cmdConfig.hooks.persistent_post_run {
true
} else {
false
}
}
config: CommandConfig{
errors: ErrorConfig{
required: cmdConfig.config.errors.required
use_custom_required: if t.config.errors.required != cmdConfig.config.errors.required {
true
} else {
false
}
value: cmdConfig.config.errors.value
use_custom_value: if t.config.errors.value != cmdConfig.config.errors.value {
true
} else {
false
}
blank: cmdConfig.config.errors.blank
use_custom_blank: if t.config.errors.blank != cmdConfig.config.errors.blank {
true
} else {
false
}
unknown: cmdConfig.config.errors.unknown
use_custom_unknown: if t.config.errors.unknown != cmdConfig.config.errors.unknown {
true
} else {
false
}
command: cmdConfig.config.errors.command
use_custom_command: if t.config.errors.command != cmdConfig.config.errors.command {
true
} else {
false
}
}
custom_help: cmdConfig.config.help
use_custom_help: if t.config.help != cmdConfig.config.help { true } else { false }
validators: ValidatorsConfig{
integer: cmdConfig.config.validators.integer
use_custom_integer: if t.config.validators.integer != cmdConfig.config.validators.integer {
true
} else {
false
}
string_var: cmdConfig.config.validators.string_var
use_custom_string_var: if t.config.validators.string_var != cmdConfig.config.validators.string_var {
true
} else {
false
}
float: cmdConfig.config.validators.float
use_custom_float: if t.config.validators.float != cmdConfig.config.validators.float {
true
} else {
false
}
boolean: cmdConfig.config.validators.boolean
use_custom_boolean: if t.config.validators.boolean != cmdConfig.config.validators.boolean {
true
} else {
false
}
}
}
}
}