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:
Turiiya 2024-05-08 13:11:40 +02:00 committed by GitHub
parent 4320f8641b
commit c0fec31bef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 69 additions and 13 deletions

View File

@ -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'))

View File

@ -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)
}
}

View File

@ -0,0 +1 @@
Command `foo` has no flag `-man`

View 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'])
}

View 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.

View 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'])
}

View File

@ -0,0 +1 @@
foo version 0.1.0

View 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'])
}

View File

@ -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}'
}