From c0fec31bef12ca36b2df70d4327e016688891f10 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Wed, 8 May 2024 13:11:40 +0200 Subject: [PATCH] cli: fix default flags when their command equivalents are disabled (#21469) * always replace `\r\n` with `\n` * fix * test * cleanup * add doc comment --- vlib/cli/cli_test.v | 8 ++---- vlib/cli/command.v | 3 +-- .../testdata/default_command_no_flag_err.out | 1 + .../testdata/default_command_no_flag_err.vv | 11 ++++++++ .../testdata/default_help_flag_no_command.out | 8 ++++++ .../testdata/default_help_flag_no_command.vv | 11 ++++++++ .../default_version_flag_no_command.out | 1 + .../default_version_flag_no_command.vv | 13 ++++++++++ vlib/cli/version.v | 26 +++++++++++++++---- 9 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 vlib/cli/testdata/default_command_no_flag_err.out create mode 100644 vlib/cli/testdata/default_command_no_flag_err.vv create mode 100644 vlib/cli/testdata/default_help_flag_no_command.out create mode 100644 vlib/cli/testdata/default_help_flag_no_command.vv create mode 100644 vlib/cli/testdata/default_version_flag_no_command.out create mode 100644 vlib/cli/testdata/default_version_flag_no_command.vv diff --git a/vlib/cli/cli_test.v b/vlib/cli/cli_test.v index 8e1ff275fa..470aec346b 100644 --- a/vlib/cli/cli_test.v +++ b/vlib/cli/cli_test.v @@ -19,12 +19,8 @@ fn test_cli_programs() { has_err = true continue } - mut expected_out := os.read_file(out_path)! - mut test_out := os.execute('${vexe} run ${test}').output - $if windows { - expected_out = expected_out.replace('\r\n', '\n') - test_out = test_out.replace('\r\n', '\n') - } + expected_out := os.read_file(out_path)!.replace('\r\n', '\n') + test_out := os.execute('${vexe} run ${test}').output.replace('\r\n', '\n') diff_ := diff.compare_text(expected_out, test_out)! if diff_ != '' { println(term.red('FAIL')) diff --git a/vlib/cli/command.v b/vlib/cli/command.v index 76b47d9dc2..ebfa9fc934 100644 --- a/vlib/cli/command.v +++ b/vlib/cli/command.v @@ -341,8 +341,7 @@ fn (cmd Command) check_version_flag() { if cmd.defaults.parsed.version.flag && cmd.version != '' && cmd.flags.contains('version') { version_flag := cmd.flags.get_bool('version') or { return } // ignore error and handle command normally if version_flag { - version_cmd := cmd.commands.get('version') or { return } // ignore error and handle command normally - version_cmd.execute(version_cmd) or { panic(err) } + print_version_for_command(cmd) or { panic(err) } exit(0) } } diff --git a/vlib/cli/testdata/default_command_no_flag_err.out b/vlib/cli/testdata/default_command_no_flag_err.out new file mode 100644 index 0000000000..f1f1b87658 --- /dev/null +++ b/vlib/cli/testdata/default_command_no_flag_err.out @@ -0,0 +1 @@ +Command `foo` has no flag `-man` diff --git a/vlib/cli/testdata/default_command_no_flag_err.vv b/vlib/cli/testdata/default_command_no_flag_err.vv new file mode 100644 index 0000000000..46c345cc5d --- /dev/null +++ b/vlib/cli/testdata/default_command_no_flag_err.vv @@ -0,0 +1,11 @@ +import cli { Command, CommandFlag } + +fn main() { + mut cmd := Command{ + name: 'foo' + defaults: struct { + man: CommandFlag{true, false} + } + } + cmd.parse(['foo', '-man']) +} diff --git a/vlib/cli/testdata/default_help_flag_no_command.out b/vlib/cli/testdata/default_help_flag_no_command.out new file mode 100644 index 0000000000..c55813db4a --- /dev/null +++ b/vlib/cli/testdata/default_help_flag_no_command.out @@ -0,0 +1,8 @@ +Usage: foo [flags] [commands] + +Flags: + -help Prints help information. + -man Prints the auto-generated manpage. + +Commands: + man Prints the auto-generated manpage. diff --git a/vlib/cli/testdata/default_help_flag_no_command.vv b/vlib/cli/testdata/default_help_flag_no_command.vv new file mode 100644 index 0000000000..f70bc3aa72 --- /dev/null +++ b/vlib/cli/testdata/default_help_flag_no_command.vv @@ -0,0 +1,11 @@ +import cli { Command, CommandFlag } + +fn main() { + mut cmd := Command{ + name: 'foo' + defaults: struct { + help: CommandFlag{false, true} + } + } + cmd.parse(['foo', '-help']) +} diff --git a/vlib/cli/testdata/default_version_flag_no_command.out b/vlib/cli/testdata/default_version_flag_no_command.out new file mode 100644 index 0000000000..d7426a8595 --- /dev/null +++ b/vlib/cli/testdata/default_version_flag_no_command.out @@ -0,0 +1 @@ +foo version 0.1.0 diff --git a/vlib/cli/testdata/default_version_flag_no_command.vv b/vlib/cli/testdata/default_version_flag_no_command.vv new file mode 100644 index 0000000000..67c38666ae --- /dev/null +++ b/vlib/cli/testdata/default_version_flag_no_command.vv @@ -0,0 +1,13 @@ +import cli { Command, CommandFlag } + +fn main() { + mut cmd := Command{ + name: 'foo' + version: '0.1.0' + posix_mode: true + defaults: struct { + version: CommandFlag{false, true} + } + } + cmd.parse(['foo', '--version']) +} diff --git a/vlib/cli/version.v b/vlib/cli/version.v index 579bead89b..354dcf78ee 100644 --- a/vlib/cli/version.v +++ b/vlib/cli/version.v @@ -14,12 +14,28 @@ fn version_cmd() Command { return Command{ name: 'version' description: 'Prints version information.' - execute: version_func + execute: print_version_for_command } } -fn version_func(version_cmd Command) ! { - cmd := version_cmd.parent - version := '${cmd.name} version ${cmd.version}' - println(version) +fn print_version_for_command(cmd Command) ! { + if cmd.args.len > 0 { + for sub_cmd in cmd.commands { + if sub_cmd.name == cmd.args[0] { + version_cmd := unsafe { &sub_cmd } + print(version_cmd.version()) + return + } + } + println('Invalid command: ${cmd.args.join(' ')}') + } else if cmd.parent != unsafe { nil } { + println(cmd.parent.version()) + } else { + println(cmd.version()) + } +} + +// version returns a generated version `string` for the `Command`. +pub fn (cmd Command) version() string { + return '${cmd.name} version ${cmd.version}' }