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', name: 'cli',
description: 'An example of the cli library', description: 'An example of the cli library',
version: '1.0.0', version: '1.0.0',
parent: 0
} }
mut greet_cmd := cli.Command{ mut greet_cmd := cli.Command{
@ -17,7 +16,6 @@ fn main() {
pre_execute: greet_pre_func, pre_execute: greet_pre_func,
execute: greet_func, execute: greet_func,
post_execute: greet_post_func, post_execute: greet_post_func,
parent: 0
} }
greet_cmd.add_flag(cli.Flag{ greet_cmd.add_flag(cli.Flag{
flag: .string, flag: .string,
@ -46,7 +44,10 @@ fn greet_func(cmd cli.Command) {
'english' { println('Hello World') } 'english' { println('Hello World') }
'german' { println('Hallo Welt') } 'german' { println('Hallo Welt') }
'dutch' { println('Hallo Wereld') } 'dutch' { println('Hallo Wereld') }
else { println('unsupported language') } else {
println('unsupported language')
break
}
} }
} }
} }

View File

@ -1,20 +1,21 @@
module cli module cli
type CallbackFn fn(cmd Command) fn nil() voidptr { return 0 }
pub struct Command { pub struct Command {
pub mut: pub mut:
name string name string
description string description string
version string version string
pre_execute CallbackFn pre_execute fn(cmd Command)
execute CallbackFn execute fn(cmd Command)
post_execute CallbackFn post_execute fn(cmd Command)
disable_help bool disable_help bool
disable_version bool disable_version bool
disable_flags bool
parent &Command parent &Command = nil()
commands []Command commands []Command
flags []Flag flags []Flag
args []string args []string
@ -43,7 +44,9 @@ pub fn (mut cmd Command) add_flag(flag Flag) {
} }
pub fn (mut cmd Command) parse(args []string) { 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.add_default_commands()
cmd.args = args[1..] cmd.args = args[1..]
@ -51,7 +54,9 @@ pub fn (mut cmd Command) parse(args []string) {
cmd.commands[i].parent = cmd cmd.commands[i].parent = cmd
} }
cmd.parse_flags() if !cmd.disable_flags {
cmd.parse_flags()
}
cmd.parse_commands() cmd.parse_commands()
} }

View File

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

View File

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