cli: run v fmt (#7440)

pull/7460/head
zakuro 2020-12-22 04:23:40 +09:00 committed by GitHub
parent 304aafdc50
commit 805ec8e177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 108 deletions

View File

@ -5,13 +5,9 @@ fn test_if_string_flag_parses() {
flag: .string flag: .string
name: 'flag' name: 'flag'
} }
flag.parse(['-flag', 'value'], false) or { flag.parse(['-flag', 'value'], false) or { panic(err) }
panic(err)
}
assert flag.value == 'value' assert flag.value == 'value'
flag.parse(['-flag=value'], false) or { flag.parse(['-flag=value'], false) or { panic(err) }
panic(err)
}
assert flag.value == 'value' assert flag.value == 'value'
} }
@ -21,40 +17,20 @@ fn test_if_bool_flag_parses() {
name: 'flag' name: 'flag'
} }
mut value := false mut value := false
flag.parse(['-flag'], false) or { flag.parse(['-flag'], false) or { panic(err) }
panic(err) value = flag.get_bool() or { panic(err) }
}
value = flag.get_bool() or {
panic(err)
}
assert value == true assert value == true
flag.parse(['-flag', 'true'], false) or { flag.parse(['-flag', 'true'], false) or { panic(err) }
panic(err) value = flag.get_bool() or { panic(err) }
}
value = flag.get_bool() or {
panic(err)
}
assert value == true assert value == true
flag.parse(['-flag=true'], false) or { flag.parse(['-flag=true'], false) or { panic(err) }
panic(err) value = flag.get_bool() or { panic(err) }
}
value = flag.get_bool() or {
panic(err)
}
assert value == true assert value == true
flag.parse(['-flag', 'false'], false) or { flag.parse(['-flag', 'false'], false) or { panic(err) }
panic(err) value = flag.get_bool() or { panic(err) }
}
value = flag.get_bool() or {
panic(err)
}
assert value == false assert value == false
flag.parse(['-flag=false'], false) or { flag.parse(['-flag=false'], false) or { panic(err) }
panic(err) value = flag.get_bool() or { panic(err) }
}
value = flag.get_bool() or {
panic(err)
}
assert value == false assert value == false
} }
@ -64,19 +40,11 @@ fn test_if_int_flag_parses() {
name: 'flag' name: 'flag'
} }
mut value := 0 mut value := 0
flag.parse(['-flag', '42'], false) or { flag.parse(['-flag', '42'], false) or { panic(err) }
panic(err) value = flag.get_int() or { panic(err) }
}
value = flag.get_int() or {
panic(err)
}
assert value == 42 assert value == 42
flag.parse(['-flag=42'], false) or { flag.parse(['-flag=42'], false) or { panic(err) }
panic(err) value = flag.get_int() or { panic(err) }
}
value = flag.get_int() or {
panic(err)
}
assert value == 42 assert value == 42
} }
@ -86,20 +54,12 @@ fn test_if_float_flag_parses() {
name: 'flag' name: 'flag'
} }
mut value := f64(0) mut value := f64(0)
flag.parse(['-flag', '3.14159'], false) or { flag.parse(['-flag', '3.14159'], false) or { panic(err) }
panic(err) value = flag.get_float() or { panic(err) }
}
value = flag.get_float() or {
panic(err)
}
assert value == 3.14159 assert value == 3.14159
flag.parse(['-flag=3.14159'], false) or { flag.parse(['-flag=3.14159'], false) or { panic(err) }
panic(err)
}
assert flag.value.f64() == 3.14159 assert flag.value.f64() == 3.14159
value = flag.get_float() or { value = flag.get_float() or { panic(err) }
panic(err)
}
assert value == 3.14159 assert value == 3.14159
} }
@ -110,18 +70,10 @@ fn test_if_flag_parses_with_abbrev() {
abbrev: 'f' abbrev: 'f'
} }
mut value := false mut value := false
flag.parse(['--flag'], true) or { flag.parse(['--flag'], true) or { panic(err) }
panic(err) value = flag.get_bool() or { panic(err) }
}
value = flag.get_bool() or {
panic(err)
}
assert value == true assert value == true
flag.parse(['-flag'], true) or { flag.parse(['-flag'], true) or { panic(err) }
panic(err) value = flag.get_bool() or { panic(err) }
}
value = flag.get_bool() or {
panic(err)
}
assert value == true assert value == true
} }

View File

@ -149,17 +149,17 @@ fn (mut cmd Command) parse_flags() {
mut found := false mut found := false
for i in 0 .. cmd.flags.len { for i in 0 .. cmd.flags.len {
unsafe { unsafe {
mut flag := &cmd.flags[i] mut flag := &cmd.flags[i]
if flag.matches(cmd.args, cmd.flags.have_abbrev()) { if flag.matches(cmd.args, cmd.flags.have_abbrev()) {
found = true found = true
flag.found = true flag.found = true
cmd.args = flag.parse(cmd.args, cmd.flags.have_abbrev()) or { cmd.args = flag.parse(cmd.args, cmd.flags.have_abbrev()) or {
println('Failed to parse flag `${cmd.args[0]}`: $err') println('Failed to parse flag `${cmd.args[0]}`: $err')
exit(1) exit(1)
}
break
} }
break
} }
}
} }
if !found { if !found {
println('Command `$cmd.name` has no flag `${cmd.args[0]}`') println('Command `$cmd.name` has no flag `${cmd.args[0]}`')
@ -221,9 +221,7 @@ fn (mut cmd Command) parse_commands() {
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') {
help_flag := cmd.flags.get_bool('help') or { help_flag := cmd.flags.get_bool('help') or { return } // ignore error and handle command normally
return
} // ignore error and handle command normally
if help_flag { if help_flag {
cmd.execute_help() cmd.execute_help()
exit(0) exit(0)
@ -233,13 +231,9 @@ fn (cmd Command) check_help_flag() {
fn (cmd Command) check_version_flag() { fn (cmd Command) check_version_flag() {
if !cmd.disable_version && cmd.version != '' && cmd.flags.contains('version') { if !cmd.disable_version && cmd.version != '' && cmd.flags.contains('version') {
version_flag := cmd.flags.get_bool('version') or { version_flag := cmd.flags.get_bool('version') or { return } // ignore error and handle command normally
return
} // ignore error and handle command normally
if version_flag { if version_flag {
version_cmd := cmd.commands.get('version') or { version_cmd := cmd.commands.get('version') or { return } // ignore error and handle command normally
return
} // ignore error and handle command normally
version_cmd.execute(version_cmd) version_cmd.execute(version_cmd)
exit(0) exit(0)
} }
@ -258,9 +252,7 @@ fn (cmd Command) check_required_flags() {
fn (cmd Command) execute_help() { fn (cmd Command) execute_help() {
if cmd.commands.contains('help') { if cmd.commands.contains('help') {
help_cmd := cmd.commands.get('help') or { help_cmd := cmd.commands.get('help') or { return } // ignore error and handle command normally
return
} // ignore error and handle command normally
help_cmd.execute(help_cmd) help_cmd.execute(help_cmd)
} else { } else {
print(cmd.help_message()) print(cmd.help_message())

View File

@ -106,9 +106,7 @@ fn test_if_multiple_flags_get_set() {
} }
fn flag_is_set_in_subcommand(cmd cli.Command) ? { fn flag_is_set_in_subcommand(cmd cli.Command) ? {
flag := cmd.flags.get_string('flag') or { flag := cmd.flags.get_string('flag') or { panic(err) }
panic(err)
}
assert flag == 'value' assert flag == 'value'
} }

View File

@ -37,9 +37,7 @@ pub fn (flags []Flag) get_bool(name string) ?bool {
} }
pub fn (flags []Flag) get_bool_or(name string, or_value bool) bool { pub fn (flags []Flag) get_bool_or(name string, or_value bool) bool {
value := flags.get_bool(name) or { value := flags.get_bool(name) or { return or_value }
return or_value
}
return value return value
} }
@ -56,9 +54,7 @@ pub fn (flags []Flag) get_int(name string) ?int {
} }
pub fn (flags []Flag) get_int_or(name string, or_value int) int { pub fn (flags []Flag) get_int_or(name string, or_value int) int {
value := flags.get_int(name) or { value := flags.get_int(name) or { return or_value }
return or_value
}
return value return value
} }
@ -75,9 +71,7 @@ pub fn (flags []Flag) get_float(name string) ?f64 {
} }
pub fn (flags []Flag) get_float_or(name string, or_value f64) f64 { pub fn (flags []Flag) get_float_or(name string, or_value f64) f64 {
value := flags.get_float(name) or { value := flags.get_float(name) or { return or_value }
return or_value
}
return value return value
} }
@ -94,9 +88,7 @@ pub fn (flags []Flag) get_string(name string) ?string {
} }
pub fn (flags []Flag) get_string_or(name string, or_value string) string { pub fn (flags []Flag) get_string_or(name string, or_value string) string {
value := flags.get_string(name) or { value := flags.get_string(name) or { return or_value }
return or_value
}
return value return value
} }