cli: improvements to help and error messages (#6375)
parent
608df40058
commit
2464deec82
|
@ -12,6 +12,7 @@ fn main() {
|
|||
mut greet_cmd := Command{
|
||||
name: 'greet'
|
||||
description: 'Prints greeting in different languages.'
|
||||
usage: '<name>'
|
||||
required_args: 1
|
||||
pre_execute: greet_pre_func
|
||||
execute: greet_func
|
||||
|
@ -44,13 +45,13 @@ fn greet_func(cmd Command) {
|
|||
name := cmd.args[0]
|
||||
for _ in 0 .. times {
|
||||
match language {
|
||||
'english' {
|
||||
'english', 'en' {
|
||||
println('Welcome $name')
|
||||
}
|
||||
'german' {
|
||||
'german', 'de' {
|
||||
println('Willkommen $name')
|
||||
}
|
||||
'dutch' {
|
||||
'dutch', 'nl' {
|
||||
println('Welkom $name')
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -269,7 +269,7 @@ fn (cmds []Command) get(name string) ?Command {
|
|||
return cmd
|
||||
}
|
||||
}
|
||||
return error('Command `$name` not found')
|
||||
return error('Command `$name` not found in $cmds')
|
||||
}
|
||||
|
||||
fn (cmds []Command) contains(name string) bool {
|
||||
|
|
|
@ -26,7 +26,7 @@ pub fn (flags []Flag) get_all_found() []Flag {
|
|||
|
||||
pub fn (flag Flag) get_bool() ?bool {
|
||||
if flag.flag != .bool {
|
||||
return error('Invalid flag type')
|
||||
return error('$flag.name: Invalid flag type `$flag.flag`, expected `bool`')
|
||||
}
|
||||
return flag.value == 'true'
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ pub fn (flags []Flag) get_bool_or(name string, or_value bool) bool {
|
|||
|
||||
pub fn (flag Flag) get_int() ?int {
|
||||
if flag.flag != .int {
|
||||
return error('Invalid flag type')
|
||||
return error('$flag.name: Invalid flag type `$flag.flag`, expected `int`')
|
||||
}
|
||||
return flag.value.int()
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ pub fn (flags []Flag) get_int_or(name string, or_value int) int {
|
|||
|
||||
pub fn (flag Flag) get_float() ?f64 {
|
||||
if flag.flag != .float {
|
||||
return error('Invalid flag type')
|
||||
return error('$flag.name: Invalid flag type `$flag.flag`, expected `float`')
|
||||
}
|
||||
return flag.value.f64()
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ pub fn (flags []Flag) get_float_or(name string, or_value f64) f64 {
|
|||
|
||||
pub fn (flag Flag) get_string() ?string {
|
||||
if flag.flag != .string {
|
||||
return error('Invalid flag type')
|
||||
return error('$flag.name: Invalid flag type `$flag.flag`, expected `string`')
|
||||
}
|
||||
return flag.value
|
||||
}
|
||||
|
@ -159,7 +159,7 @@ fn (flags []Flag) get(name string) ?Flag {
|
|||
return flag
|
||||
}
|
||||
}
|
||||
return error('Flag `$name` not found')
|
||||
return error('Flag `$name` not found in $flags')
|
||||
}
|
||||
|
||||
fn (flags []Flag) contains(name string) bool {
|
||||
|
|
|
@ -63,15 +63,16 @@ fn (cmd Command) help_message() string {
|
|||
if cmd.commands.len > 0 {
|
||||
help += ' [commands]'
|
||||
}
|
||||
if cmd.usage.len > 0 {
|
||||
help += ' $cmd.usage'
|
||||
} else {
|
||||
for i in 0 .. cmd.required_args {
|
||||
help += ' <arg$i>'
|
||||
}
|
||||
if cmd.usage.len > 0 {
|
||||
help += ' $cmd.usage'
|
||||
}
|
||||
help += '\n\n'
|
||||
help += '\n'
|
||||
if cmd.description != '' {
|
||||
help += '$cmd.description\n\n'
|
||||
help += '\n$cmd.description\n'
|
||||
}
|
||||
mut abbrev_len := 0
|
||||
mut name_len := min_description_indent_len
|
||||
|
@ -92,7 +93,7 @@ fn (cmd Command) help_message() string {
|
|||
}
|
||||
}
|
||||
if cmd.flags.len > 0 {
|
||||
help += 'Flags:\n'
|
||||
help += '\nFlags:\n'
|
||||
for flag in cmd.flags {
|
||||
mut flag_name := ''
|
||||
if flag.abbrev != '' && cmd.flags.have_abbrev() {
|
||||
|
@ -113,17 +114,15 @@ fn (cmd Command) help_message() string {
|
|||
help += '$base_indent$flag_name$description_indent' + pretty_description(flag.description +
|
||||
required, base_indent_len + name_len) + '\n'
|
||||
}
|
||||
help += '\n'
|
||||
}
|
||||
if cmd.commands.len > 0 {
|
||||
help += 'Commands:\n'
|
||||
help += '\nCommands:\n'
|
||||
for command in cmd.commands {
|
||||
base_indent := ' '.repeat(base_indent_len)
|
||||
description_indent := ' '.repeat(name_len - command.name.len)
|
||||
help += '$base_indent$command.name$description_indent' + pretty_description(command.description, name_len) +
|
||||
'\n'
|
||||
}
|
||||
help += '\n'
|
||||
}
|
||||
return help
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue