mirror of
https://github.com/vlang/v.git
synced 2025-09-09 23:39:39 -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
|
has_err = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mut expected_out := os.read_file(out_path)!
|
expected_out := os.read_file(out_path)!.replace('\r\n', '\n')
|
||||||
mut test_out := os.execute('${vexe} run ${test}').output
|
test_out := os.execute('${vexe} run ${test}').output.replace('\r\n', '\n')
|
||||||
$if windows {
|
|
||||||
expected_out = expected_out.replace('\r\n', '\n')
|
|
||||||
test_out = test_out.replace('\r\n', '\n')
|
|
||||||
}
|
|
||||||
diff_ := diff.compare_text(expected_out, test_out)!
|
diff_ := diff.compare_text(expected_out, test_out)!
|
||||||
if diff_ != '' {
|
if diff_ != '' {
|
||||||
println(term.red('FAIL'))
|
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') {
|
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
|
version_flag := cmd.flags.get_bool('version') or { return } // ignore error and handle command normally
|
||||||
if version_flag {
|
if version_flag {
|
||||||
version_cmd := cmd.commands.get('version') or { return } // ignore error and handle command normally
|
print_version_for_command(cmd) or { panic(err) }
|
||||||
version_cmd.execute(version_cmd) or { panic(err) }
|
|
||||||
exit(0)
|
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{
|
return Command{
|
||||||
name: 'version'
|
name: 'version'
|
||||||
description: 'Prints version information.'
|
description: 'Prints version information.'
|
||||||
execute: version_func
|
execute: print_version_for_command
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn version_func(version_cmd Command) ! {
|
fn print_version_for_command(cmd Command) ! {
|
||||||
cmd := version_cmd.parent
|
if cmd.args.len > 0 {
|
||||||
version := '${cmd.name} version ${cmd.version}'
|
for sub_cmd in cmd.commands {
|
||||||
println(version)
|
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