cli: add pre_execute & post_execute
parent
f724a4d820
commit
f1274e34c2
|
@ -16,7 +16,9 @@ fn main() {
|
||||||
mut greet_cmd := cli.Command{
|
mut greet_cmd := cli.Command{
|
||||||
name: 'greet',
|
name: 'greet',
|
||||||
description: 'Prints greeting in different languages',
|
description: 'Prints greeting in different languages',
|
||||||
|
pre_execute: greet_pre_func,
|
||||||
execute: greet_func,
|
execute: greet_func,
|
||||||
|
post_execute: greet_post_func,
|
||||||
parent: 0
|
parent: 0
|
||||||
}
|
}
|
||||||
greet_cmd.add_flag(cli.Flag{
|
greet_cmd.add_flag(cli.Flag{
|
||||||
|
@ -50,3 +52,11 @@ fn greet_func(cmd cli.Command) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn greet_pre_func(cmd cli.Command) {
|
||||||
|
println('This is a function running before the main function')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn greet_post_func(cmd cli.Command) {
|
||||||
|
println('This is a function running after the main function')
|
||||||
|
}
|
|
@ -5,7 +5,9 @@ pub mut:
|
||||||
name string
|
name string
|
||||||
description string
|
description string
|
||||||
version string
|
version string
|
||||||
|
pre_execute fn(cmd Command)
|
||||||
execute fn(cmd Command)
|
execute fn(cmd Command)
|
||||||
|
post_execute fn(cmd Command)
|
||||||
|
|
||||||
disable_help bool
|
disable_help bool
|
||||||
disable_version bool
|
disable_version bool
|
||||||
|
@ -126,8 +128,18 @@ fn (cmd &Command) parse_commands() {
|
||||||
} else {
|
} else {
|
||||||
cmd.check_required_flags()
|
cmd.check_required_flags()
|
||||||
|
|
||||||
|
if int(cmd.pre_execute) > 0 {
|
||||||
|
pre_execute := cmd.pre_execute
|
||||||
|
pre_execute(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
execute := cmd.execute
|
execute := cmd.execute
|
||||||
execute(cmd) // TODO: fix once higher order function can be execute on struct variable
|
execute(cmd) // TODO: fix once higher order function can be execute on struct variable
|
||||||
|
|
||||||
|
if int(cmd.post_execute) > 0 {
|
||||||
|
post_execute := cmd.post_execute
|
||||||
|
post_execute(cmd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue