cli: help displayed when a Command has no execute function (fix #6530) (#6567)

pull/6573/head
shnorbluk 2020-10-07 05:39:13 +02:00 committed by GitHub
parent ce302c29e4
commit b940dc4f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 25 deletions

View File

@ -186,36 +186,32 @@ fn (mut cmd Command) parse_commands() {
} }
} }
// if no further command was found, execute current command // if no further command was found, execute current command
if int(cmd.execute) == 0 {
if !cmd.disable_help {
cmd.execute_help()
}
} else {
if cmd.required_args > 0 { if cmd.required_args > 0 {
if cmd.required_args > cmd.args.len { if cmd.required_args > cmd.args.len {
println('Command `$cmd.name` needs at least $cmd.required_args arguments') eprintln('Command `$cmd.name` needs at least $cmd.required_args arguments')
exit(1) exit(1)
} }
} }
cmd.check_required_flags() cmd.check_required_flags()
if int(cmd.pre_execute) > 0 { if int(cmd.pre_execute) > 0 {
cmd.pre_execute(*cmd) or { cmd.pre_execute(*cmd) or {
println('cli preexecution error: $err') eprintln('cli preexecution error: $err')
exit(1) exit(1)
} }
} }
if int(cmd.execute) > 0 {
cmd.execute(*cmd) or { cmd.execute(*cmd) or {
println('cli execution error: $err') eprintln('cli execution error: $err')
exit(1) exit(1)
} }
}
if int(cmd.post_execute) > 0 { if int(cmd.post_execute) > 0 {
cmd.post_execute(*cmd) or { cmd.post_execute(*cmd) or {
println('cli postexecution error: $err') eprintln('cli postexecution error: $err')
exit(1) exit(1)
} }
} }
} }
}
fn (cmd Command) check_help_flag() { fn (cmd Command) check_help_flag() {
if !cmd.disable_help && cmd.flags.contains('help') { if !cmd.disable_help && cmd.flags.contains('help') {

View File

@ -0,0 +1,7 @@
import os
import cli {Command}
fn main() {
mut cmd := Command {}
cmd.parse(os.args)
}