From d688d356a71255c6dae01fd53ab277402b0793dc Mon Sep 17 00:00:00 2001 From: Tim Basel Date: Thu, 28 May 2020 13:32:43 +0200 Subject: [PATCH] cli: assign parent by default; add disable_flag --- examples/cli.v | 7 ++++--- vlib/cli/command.v | 19 ++++++++++++------- vlib/cli/help.v | 1 - vlib/cli/version.v | 1 - 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/cli.v b/examples/cli.v index 4d9eceabea..1d61c9df9a 100644 --- a/examples/cli.v +++ b/examples/cli.v @@ -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 + } } } } diff --git a/vlib/cli/command.v b/vlib/cli/command.v index f8e6c9da9f..b043dd24da 100644 --- a/vlib/cli/command.v +++ b/vlib/cli/command.v @@ -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() } diff --git a/vlib/cli/help.v b/vlib/cli/help.v index 84f453a2ee..0ce2619459 100644 --- a/vlib/cli/help.v +++ b/vlib/cli/help.v @@ -23,7 +23,6 @@ fn help_cmd() Command { name: 'help', description: 'Prints help information', execute: help_func, - parent: 0 } } diff --git a/vlib/cli/version.v b/vlib/cli/version.v index 42a1d8d47d..95b961c3a1 100644 --- a/vlib/cli/version.v +++ b/vlib/cli/version.v @@ -14,7 +14,6 @@ fn version_cmd() Command { name: 'version' description: 'Prints version information', execute: version_func, - parent: 0 } }