vlib/cli/help_test.v
Rama master · réplica de lectura del repositorio original.
module cli
fn test_help_message() {
mut cmd := Command{
name: 'command'
description: 'description'
commands: [
Command{
name: 'sub'
description: 'subcommand'
},
Command{
name: 'sub2'
description: 'another subcommand'
},
]
flags: [
Flag{
flag: .string
name: 'str'
description: 'str flag'
},
Flag{
flag: .bool
name: 'bool'
description: 'bool flag'
abbrev: 'b'
},
Flag{
flag: .string
name: 'required'
abbrev: 'r'
required: true
},
]
}
assert cmd.help_message() == r'Usage: command [flags] [commands]
description
Flags:
-str str flag
-b -bool bool flag
-r -required (required)
Commands:
sub subcommand
sub2 another subcommand
'
cmd.posix_mode = true
assert cmd.help_message() == r'Usage: command [flags] [commands]
description
Flags:
--str str flag
-b --bool bool flag
-r --required (required)
Commands:
sub subcommand
sub2 another subcommand
'
}
fn test_help_message_groups_commands_by_group_field() {
cmd := Command{
name: 'app'
description: 'app'
commands: [
Command{
name: 'auth'
description: 'Authenticate'
group: 'Core commands'
},
Command{
name: 'repo'
description: 'Work with repositories'
group: 'Core commands'
},
Command{
name: 'alias'
description: 'Define shortcuts'
group: 'General commands'
},
Command{
name: 'misc'
description: 'Other things'
},
]
}
assert cmd.help_message() == r'Usage: app [commands]
app
Core commands:
auth Authenticate
repo Work with repositories
General commands:
alias Define shortcuts
Commands:
misc Other things
'
}
fn test_help_message_renders_group_title_verbatim() {
// Group string is rendered as-is; capitalisation is the caller's
// choice. Non-ASCII first characters must work without mojibake.
cmd := Command{
name: 'app'
commands: [
Command{
name: 'edit'
description: 'Edit something'
group: 'éditeurs'
},
]
}
assert cmd.help_message() == r'Usage: app [commands]
éditeurs:
edit Edit something
'
}
fn test_help_message_separates_inherited_flags_from_locals() {
mut root := Command{
name: 'app'
flags: [
Flag{
flag: .string
name: 'config'
abbrev: 'c'
description: 'Path to config'
global: true
},
]
commands: [
Command{
name: 'run'
description: 'Run something'
flags: [
Flag{
flag: .bool
name: 'verbose'
abbrev: 'v'
description: 'Verbose output'
},
]
},
]
}
root.setup()
mut sub := root.commands[0]
sub.parent = unsafe { &root }
// Mirror what `parse_commands` does when a child is dispatched.
sub.flags << root.flags[0]
assert sub.help_message() == r'Usage: app run [flags]
Run something
Flags:
-v -verbose Verbose output
Inherited flags:
-c -config Path to config
'
}
fn test_help_message_renders_examples_and_learn_more() {
cmd := Command{
name: 'app'
description: 'app'
examples: ['\$ app run', '\$ app help run']
learn_more: 'Use `app help <command>` for details about a command.\nDocumentation lives at https://example.test'
}
assert cmd.help_message() == r'Usage: app
app
Examples:
$ app run
$ app help run
Learn more:
Use `app help <command>` for details about a command.
Documentation lives at https://example.test
'
}
fn test_help_message_omits_empty_optional_sections() {
cmd := Command{
name: 'app'
}
// No description, no flags, no commands, no examples, no learn_more.
assert cmd.help_message() == 'Usage: app\n'
}
fn test_help_message_long_flag_before_abbreviation() {
for posix_mode in [false, true] {
mut cmd := Command{
name: 'tool'
posix_mode: posix_mode
flags: [
Flag{ flag: .bool, name: 'allow-weak-password', description: 'Accept a short password.' },
Flag{ flag: .string, name: 'output', abbrev: 'o', description: 'Write the result.' },
]
commands: [Command{ name: 'run', description: 'Run the tool.' }]
}
help := cmd.help_message()
if posix_mode {
assert help.contains(' --allow-weak-password Accept a short password.\n')
assert help.contains(' -o --output Write the result.\n')
assert help.contains(' run Run the tool.\n')
} else {
assert help.contains(' -allow-weak-password Accept a short password.\n')
assert help.contains(' -o -output Write the result.\n')
assert help.contains(' run Run the tool.\n')
}
cmd.flags.reverse_in_place()
reordered := cmd.help_message()
for line in help.split_into_lines() {
assert line in reordered.split_into_lines()
}
}
}
fn test_help_message_long_flag_before_automatic_help_abbreviation() {
mut cmd := Command{
name: 'tool'
posix_mode: true
flags: [Flag{ flag: .bool, name: 'allow-weak-password', description: 'Accept a short password.' }]
}
cmd.parse_defaults()
help := cmd.help_message()
assert help.contains(' --allow-weak-password Accept a short password.\n')
assert help.contains(' -h --help Print help information\n')
assert help.contains(' --man Print the auto-generated manpage\n')
}