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