cli: help/version abbrev optional + fix various inconsistencies (#5586)

pull/5581/head
Tim Basel 2020-07-01 10:54:34 +02:00 committed by GitHub
parent ca1f47a742
commit 7386adfc99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 24 deletions

View File

@ -61,11 +61,11 @@ pub fn (mut cmd Command) parse(args []string) {
}
fn (mut cmd Command) add_default_flags() {
if !cmd.disable_help && !cmd.flags.contains('help') && !cmd.flags.contains('h') {
cmd.add_flag(help_flag())
if !cmd.disable_help && !cmd.flags.contains('help') {
cmd.add_flag(help_flag(!cmd.flags.contains('h')))
}
if !cmd.disable_version && cmd.version != '' && !cmd.flags.contains('version') && !cmd.flags.contains('v') {
cmd.add_flag(version_flag())
if !cmd.disable_version && cmd.version != '' && !cmd.flags.contains('version') {
cmd.add_flag(version_flag(!cmd.flags.contains('v')))
}
}
@ -88,11 +88,10 @@ fn (mut cmd Command) parse_flags() {
mut flag := &cmd.flags[i]
if flag.matches(cmd.args) {
found = true
args := flag.parse(cmd.args) or { // TODO: fix once options types can be assigned to struct variables
cmd.args = flag.parse(cmd.args) or {
println('failed to parse flag ${cmd.args[0]}: ${err}')
exit(1)
}
cmd.args = args
break
}
}
@ -104,9 +103,8 @@ fn (mut cmd Command) parse_flags() {
}
}
fn (cmd &Command) parse_commands() {
flags := cmd.flags
global_flags := flags.filter(it.global) // TODO: fix once filter can be applied to struct variable
fn (mut cmd Command) parse_commands() {
global_flags := cmd.flags.filter(it.global)
cmd.check_help_flag()
cmd.check_version_flag()
@ -125,7 +123,7 @@ fn (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 {
help_cmd := cmd.commands.get('help') or { return } // ignore error and handle command normally
@ -136,21 +134,18 @@ fn (cmd &Command) parse_commands() {
cmd.check_required_flags()
if int(cmd.pre_execute) > 0 {
pre_execute := cmd.pre_execute
pre_execute(cmd)
cmd.pre_execute(*cmd)
}
execute := cmd.execute
execute(cmd) // TODO: fix once higher order function can be execute on struct variable
cmd.execute(*cmd)
if int(cmd.post_execute) > 0 {
post_execute := cmd.post_execute
post_execute(cmd)
cmd.post_execute(*cmd)
}
}
}
fn (cmd &Command) check_help_flag() {
fn (mut cmd Command) check_help_flag() {
if cmd.disable_help {
return
}
@ -165,7 +160,7 @@ fn (cmd &Command) check_help_flag() {
}
}
fn (cmd &Command) check_version_flag() {
fn (mut cmd Command) check_version_flag() {
if cmd.disable_version {
return
}
@ -180,7 +175,7 @@ fn (cmd &Command) check_version_flag() {
}
}
fn (cmd &Command) check_required_flags() {
fn (mut cmd Command) check_required_flags() {
for flag in cmd.flags {
if flag.required && flag.value == '' {
full_name := cmd.full_name()

View File

@ -79,7 +79,7 @@ fn (mut flag Flag) parse(args []string) ?[]string {
}
// check if first arg matches flag
fn (flag &Flag) matches(args []string) bool {
fn (mut flag Flag) matches(args []string) bool {
return
(flag.name != '' && args[0].starts_with('--${flag.name}')) ||
(flag.abbrev != '' && args[0].starts_with('-${flag.abbrev}'))

View File

@ -9,11 +9,11 @@ const (
spacing = 2
)
fn help_flag() Flag {
fn help_flag(with_abbrev bool) Flag {
return Flag{
flag: .bool,
name: 'help',
abbrev: 'h',
abbrev: if with_abbrev { 'h' } else { '' },
description: 'Prints help information',
}
}

View File

@ -1,10 +1,10 @@
module cli
fn version_flag() Flag {
fn version_flag(with_abbrev bool) Flag {
return Flag{
flag: .bool,
name: 'version',
abbrev: 'v',
abbrev: if with_abbrev { 'v' } else { '' },
description: 'Prints version information',
}
}