cli: assign parent by default; add disable_flag

pull/5096/head
Tim Basel 2020-05-28 13:32:43 +02:00 committed by GitHub
parent 977eb895e8
commit d688d356a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 12 deletions

View File

@ -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
}
}
}
}

View File

@ -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) {
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
}
if !cmd.disable_flags {
cmd.parse_flags()
}
cmd.parse_commands()
}

View File

@ -23,7 +23,6 @@ fn help_cmd() Command {
name: 'help',
description: 'Prints help information',
execute: help_func,
parent: 0
}
}

View File

@ -14,7 +14,6 @@ fn version_cmd() Command {
name: 'version'
description: 'Prints version information',
execute: version_func,
parent: 0
}
}