From dea8662d6bd5e72af7113d181c5144319b414afe Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Sat, 17 Oct 2020 18:06:27 +0200 Subject: [PATCH] cli: show help for empty root command (#6636) --- vlib/cli/command.v | 6 ++++++ vlib/v/tests/inout/cli_command_no_execute.vv | 10 +++++++--- vlib/v/tests/inout/cli_root_default_help.out | 7 +++++++ vlib/v/tests/inout/cli_root_default_help.vv | 7 +++++++ 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/inout/cli_root_default_help.out create mode 100644 vlib/v/tests/inout/cli_root_default_help.vv diff --git a/vlib/cli/command.v b/vlib/cli/command.v index f2e528c0c6..268fceb3ea 100644 --- a/vlib/cli/command.v +++ b/vlib/cli/command.v @@ -185,6 +185,12 @@ fn (mut cmd Command) parse_commands() { } } } + if cmd.is_root() && int(cmd.execute) == 0 { + if !cmd.disable_help { + cmd.execute_help() + return + } + } // if no further command was found, execute current command if cmd.required_args > 0 { if cmd.required_args > cmd.args.len { diff --git a/vlib/v/tests/inout/cli_command_no_execute.vv b/vlib/v/tests/inout/cli_command_no_execute.vv index db532200c8..ba6dfe5562 100644 --- a/vlib/v/tests/inout/cli_command_no_execute.vv +++ b/vlib/v/tests/inout/cli_command_no_execute.vv @@ -1,7 +1,11 @@ -import os import cli {Command} fn main() { - mut cmd := Command {} - cmd.parse(os.args) + mut cmd := Command { + name: 'cmd' + } + cmd.add_command({ + name: 'foo' + }) + cmd.parse(['', 'cmd', 'foo']) } diff --git a/vlib/v/tests/inout/cli_root_default_help.out b/vlib/v/tests/inout/cli_root_default_help.out new file mode 100644 index 0000000000..f8cfda7f5d --- /dev/null +++ b/vlib/v/tests/inout/cli_root_default_help.out @@ -0,0 +1,7 @@ +Usage: [flags] [commands] + +Flags: + -help Prints help information. + +Commands: + help Prints help information. diff --git a/vlib/v/tests/inout/cli_root_default_help.vv b/vlib/v/tests/inout/cli_root_default_help.vv new file mode 100644 index 0000000000..384624ac71 --- /dev/null +++ b/vlib/v/tests/inout/cli_root_default_help.vv @@ -0,0 +1,7 @@ +import cli {Command} +import os + +fn main() { + mut cmd := Command {} + cmd.parse(os.args) +}