mirror of
https://github.com/vlang/v.git
synced 2025-09-08 06:41:58 -04:00
cli: fix default flags when their command equivalents are disabled (#21469)
* always replace `\r\n` with `\n` * fix * test * cleanup * add doc comment
This commit is contained in:
parent
4320f8641b
commit
c0fec31bef
@ -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'))
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
1
vlib/cli/testdata/default_command_no_flag_err.out
vendored
Normal file
1
vlib/cli/testdata/default_command_no_flag_err.out
vendored
Normal file
@ -0,0 +1 @@
|
||||
Command `foo` has no flag `-man`
|
11
vlib/cli/testdata/default_command_no_flag_err.vv
vendored
Normal file
11
vlib/cli/testdata/default_command_no_flag_err.vv
vendored
Normal file
@ -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'])
|
||||
}
|
8
vlib/cli/testdata/default_help_flag_no_command.out
vendored
Normal file
8
vlib/cli/testdata/default_help_flag_no_command.out
vendored
Normal file
@ -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.
|
11
vlib/cli/testdata/default_help_flag_no_command.vv
vendored
Normal file
11
vlib/cli/testdata/default_help_flag_no_command.vv
vendored
Normal file
@ -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'])
|
||||
}
|
1
vlib/cli/testdata/default_version_flag_no_command.out
vendored
Normal file
1
vlib/cli/testdata/default_version_flag_no_command.out
vendored
Normal file
@ -0,0 +1 @@
|
||||
foo version 0.1.0
|
13
vlib/cli/testdata/default_version_flag_no_command.vv
vendored
Normal file
13
vlib/cli/testdata/default_version_flag_no_command.vv
vendored
Normal file
@ -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'])
|
||||
}
|
@ -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}'
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user