flag: fix panic if flag value was not provided (#6478)
parent
ae48b709ed
commit
aa1d5fcbdd
|
@ -113,13 +113,13 @@ fn (mut fs FlagParser) parse_value(longhand string, shorthand byte) []string {
|
|||
}
|
||||
if (arg.len == 2 && arg[0] == `-` && arg[1] == shorthand ) || arg == full {
|
||||
if i+1 >= fs.args.len {
|
||||
panic("Missing argument for '$longhand'")
|
||||
return []
|
||||
}
|
||||
nextarg := fs.args[i+1]
|
||||
if nextarg.len > 2 && nextarg[..2] == '--' {
|
||||
//It could be end of input (--) or another argument (--abc).
|
||||
//Both are invalid so die.
|
||||
panic("Missing argument for '$longhand'")
|
||||
return []
|
||||
}
|
||||
found_entries << fs.args[i+1]
|
||||
to_delete << i
|
||||
|
|
|
@ -352,3 +352,13 @@ fn test_single_dash() {
|
|||
flag_update := fp.bool('update', `u`, false, 'Update tools')
|
||||
assert flag_update == false
|
||||
}
|
||||
|
||||
fn test_optional_flags() {
|
||||
mut fp := flag.new_flag_parser(['-a', '10', '-b'])
|
||||
a := fp.int_opt('some-flag', `a`, '') or {
|
||||
assert false
|
||||
return
|
||||
}
|
||||
b := fp.string_opt('another-flag', `b`, '') or { 'some_default_value' }
|
||||
assert b == 'some_default_value'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue