cli: assign parent by default; add disable_flag
parent
977eb895e8
commit
d688d356a7
|
@ -8,7 +8,6 @@ fn main() {
|
|||
name: 'cli',
|
||||
description: 'An example of the cli library',
|
||||
version: '1.0.0',
|
||||
parent: 0
|
||||
}
|
||||
|
||||
mut greet_cmd := cli.Command{
|
||||
|
@ -17,7 +16,6 @@ fn main() {
|
|||
pre_execute: greet_pre_func,
|
||||
execute: greet_func,
|
||||
post_execute: greet_post_func,
|
||||
parent: 0
|
||||
}
|
||||
greet_cmd.add_flag(cli.Flag{
|
||||
flag: .string,
|
||||
|
@ -46,7 +44,10 @@ fn greet_func(cmd cli.Command) {
|
|||
'english' { println('Hello World') }
|
||||
'german' { println('Hallo Welt') }
|
||||
'dutch' { println('Hallo Wereld') }
|
||||
else { println('unsupported language') }
|
||||
else {
|
||||
println('unsupported language')
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,21 @@
|
|||
module cli
|
||||
|
||||
type CallbackFn fn(cmd Command)
|
||||
fn nil() voidptr { return 0 }
|
||||
|
||||
pub struct Command {
|
||||
pub mut:
|
||||
name string
|
||||
description string
|
||||
version string
|
||||
pre_execute CallbackFn
|
||||
execute CallbackFn
|
||||
post_execute CallbackFn
|
||||
pre_execute fn(cmd Command)
|
||||
execute fn(cmd Command)
|
||||
post_execute fn(cmd Command)
|
||||
|
||||
disable_help bool
|
||||
disable_version bool
|
||||
disable_flags bool
|
||||
|
||||
parent &Command
|
||||
parent &Command = nil()
|
||||
commands []Command
|
||||
flags []Flag
|
||||
args []string
|
||||
|
@ -43,7 +44,9 @@ pub fn (mut cmd Command) add_flag(flag Flag) {
|
|||
}
|
||||
|
||||
pub fn (mut cmd Command) parse(args []string) {
|
||||
cmd.add_default_flags()
|
||||
if !cmd.disable_flags {
|
||||
cmd.add_default_flags()
|
||||
}
|
||||
cmd.add_default_commands()
|
||||
|
||||
cmd.args = args[1..]
|
||||
|
@ -51,7 +54,9 @@ pub fn (mut cmd Command) parse(args []string) {
|
|||
cmd.commands[i].parent = cmd
|
||||
}
|
||||
|
||||
cmd.parse_flags()
|
||||
if !cmd.disable_flags {
|
||||
cmd.parse_flags()
|
||||
}
|
||||
cmd.parse_commands()
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,6 @@ fn help_cmd() Command {
|
|||
name: 'help',
|
||||
description: 'Prints help information',
|
||||
execute: help_func,
|
||||
parent: 0
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@ fn version_cmd() Command {
|
|||
name: 'version'
|
||||
description: 'Prints version information',
|
||||
execute: version_func,
|
||||
parent: 0
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue