flag: run vfmt over vlib/flag, add it to vtest-cleancode.v

pull/7460/head
Delyan Angelov 2020-12-21 22:24:10 +02:00
parent 5cd2dffafb
commit 9eb6c4ef87
3 changed files with 136 additions and 135 deletions

View File

@ -21,6 +21,7 @@ const (
'cmd/tools/vdoc.v', 'cmd/tools/vdoc.v',
'cmd/v/v.v', 'cmd/v/v.v',
'vlib/builtin/array.v', 'vlib/builtin/array.v',
'vlib/builtin/array_test.v',
'vlib/builtin/map.v', 'vlib/builtin/map.v',
'vlib/math/bits/bits.v', 'vlib/math/bits/bits.v',
'vlib/time/time.v', 'vlib/time/time.v',
@ -56,12 +57,13 @@ const (
'vlib/v/util/', 'vlib/v/util/',
'vlib/v/vet/', 'vlib/v/vet/',
'vlib/v/vmod/', 'vlib/v/vmod/',
'vlib/cli/',
'vlib/flag/',
'vlib/gg/gg.v', 'vlib/gg/gg.v',
'vlib/os/', 'vlib/os/',
'vlib/semver/', 'vlib/semver/',
'vlib/strings/', 'vlib/strings/',
'vlib/time/', 'vlib/time/',
'vlib/builtin/array_test.v',
] ]
) )

View File

@ -187,9 +187,7 @@ fn (mut fs FlagParser) parse_bool_value(longhand string, shorthand byte) ?string
// In the situation that the flag was not provided, it returns null. // In the situation that the flag was not provided, it returns null.
pub fn (mut fs FlagParser) bool_opt(name string, abbr byte, usage string) ?bool { pub fn (mut fs FlagParser) bool_opt(name string, abbr byte, usage string) ?bool {
fs.add_flag(name, abbr, usage, '<bool>') fs.add_flag(name, abbr, usage, '<bool>')
parsed := fs.parse_bool_value(name, abbr) or { parsed := fs.parse_bool_value(name, abbr) or { return error("parameter '$name' not provided") }
return error("parameter '$name' not provided")
}
return parsed == 'true' return parsed == 'true'
} }
@ -201,9 +199,7 @@ pub fn (mut fs FlagParser) bool_opt(name string, abbr byte, usage string) ?bool
// version with abbr // version with abbr
// TODO error handling for invalid string to bool conversion // TODO error handling for invalid string to bool conversion
pub fn (mut fs FlagParser) bool(name string, abbr byte, bdefault bool, usage string) bool { pub fn (mut fs FlagParser) bool(name string, abbr byte, bdefault bool, usage string) bool {
value := fs.bool_opt(name, abbr, usage) or { value := fs.bool_opt(name, abbr, usage) or { return bdefault }
return bdefault
}
return value return value
} }
@ -238,9 +234,7 @@ pub fn (mut fs FlagParser) int_opt(name string, abbr byte, usage string) ?int {
// version with abbr // version with abbr
// TODO error handling for invalid string to int conversion // TODO error handling for invalid string to int conversion
pub fn (mut fs FlagParser) int(name string, abbr byte, idefault int, usage string) int { pub fn (mut fs FlagParser) int(name string, abbr byte, idefault int, usage string) int {
value := fs.int_opt(name, abbr, usage) or { value := fs.int_opt(name, abbr, usage) or { return idefault }
return idefault
}
return value return value
} }
@ -275,9 +269,7 @@ pub fn (mut fs FlagParser) float_opt(name string, abbr byte, usage string) ?f64
// version with abbr // version with abbr
// TODO error handling for invalid string to float conversion // TODO error handling for invalid string to float conversion
pub fn (mut fs FlagParser) float(name string, abbr byte, fdefault f64, usage string) f64 { pub fn (mut fs FlagParser) float(name string, abbr byte, fdefault f64, usage string) f64 {
value := fs.float_opt(name, abbr, usage) or { value := fs.float_opt(name, abbr, usage) or { return fdefault }
return fdefault
}
return value return value
} }
@ -306,9 +298,7 @@ pub fn (mut fs FlagParser) string_opt(name string, abbr byte, usage string) ?str
// the default value is returned // the default value is returned
// version with abbr // version with abbr
pub fn (mut fs FlagParser) string(name string, abbr byte, sdefault string, usage string) string { pub fn (mut fs FlagParser) string(name string, abbr byte, sdefault string, usage string) string {
value := fs.string_opt(name, abbr, usage) or { value := fs.string_opt(name, abbr, usage) or { return sdefault }
return sdefault
}
return value return value
} }

View File

@ -2,101 +2,101 @@ import flag
fn test_if_flag_not_given_return_default_values() { fn test_if_flag_not_given_return_default_values() {
mut fp := flag.new_flag_parser([]) mut fp := flag.new_flag_parser([])
assert false == fp.bool('a_bool', 0, false, '') assert false == fp.bool('a_bool', 0, false, '')
&& 42 == fp.int('an_int', 0, 42, '') assert 42 == fp.int('an_int', 0, 42, '')
&& 1.0 == fp.float('a_float', 0, 1.0, '') assert 1.0 == fp.float('a_float', 0, 1.0, '')
&& 'stuff' == fp.string('a_string', 0, 'stuff', '') assert 'stuff' == fp.string('a_string', 0, 'stuff', '')
} }
fn test_could_define_application_name_and_version() { fn test_could_define_application_name_and_version() {
mut fp := flag.new_flag_parser([]) mut fp := flag.new_flag_parser([])
fp.application('test app') fp.application('test app')
fp.version('0.0.42') fp.version('0.0.42')
fp.description('some text') fp.description('some text')
assert fp.application_name == 'test app' assert fp.application_name == 'test app'
&& fp.application_version == '0.0.42' assert fp.application_version == '0.0.42'
&& fp.application_description == 'some text' assert fp.application_description == 'some text'
} }
fn test_bool_flags_do_not_need_an_value() { fn test_bool_flags_do_not_need_an_value() {
mut fp := flag.new_flag_parser(['--a_bool']) mut fp := flag.new_flag_parser(['--a_bool'])
assert true == fp.bool('a_bool', 0, false, '') assert true == fp.bool('a_bool', 0, false, '')
} }
fn test_flags_could_be_defined_with_eq() { fn test_flags_could_be_defined_with_eq() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--an_int=42', '--an_int=42',
'--a_float=2.0', '--a_float=2.0',
'--bool_without', '--bool_without',
'--a_string=stuff', '--a_string=stuff',
'--a_bool=true']) '--a_bool=true',
])
assert 42 == fp.int('an_int', 0, 0o666, '') assert 42 == fp.int('an_int', 0, 0o666, '')
&& true == fp.bool('a_bool', 0, false, '') assert true == fp.bool('a_bool', 0, false, '')
&& true == fp.bool('bool_without', 0, false, '') assert true == fp.bool('bool_without', 0, false, '')
&& 2.0 == fp.float('a_float', 0, 1.0, '') assert 2.0 == fp.float('a_float', 0, 1.0, '')
&& 'stuff' == fp.string('a_string', 0, 'not_stuff', '') assert 'stuff' == fp.string('a_string', 0, 'not_stuff', '')
} }
fn test_values_could_be_defined_without_eq() { fn test_values_could_be_defined_without_eq() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--an_int', '42', '--an_int',
'--a_float', '2.0', '42',
'--bool_without', '--a_float',
'--a_string', 'stuff', '2.0',
'--a_bool', 'true']) '--bool_without',
'--a_string',
'stuff',
'--a_bool',
'true',
])
assert 42 == fp.int('an_int', 0, 0o666, '') assert 42 == fp.int('an_int', 0, 0o666, '')
&& true == fp.bool('a_bool', 0, false, '') assert true == fp.bool('a_bool', 0, false, '')
&& true == fp.bool('bool_without', 0, false, '') assert true == fp.bool('bool_without', 0, false, '')
&& 2.0 == fp.float('a_float', 0, 1.0, '') assert 2.0 == fp.float('a_float', 0, 1.0, '')
&& 'stuff' == fp.string('a_string', 0, 'not_stuff', '') assert 'stuff' == fp.string('a_string', 0, 'not_stuff', '')
} }
fn test_values_could_be_defined_mixed() { fn test_values_could_be_defined_mixed() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--an_int', '42', '--an_int',
'--a_float=2.0', '42',
'--bool_without', '--a_float=2.0',
'--a_string', 'stuff', '--bool_without',
'--a_bool=true']) '--a_string',
'stuff',
'--a_bool=true',
])
assert 42 == fp.int('an_int', 0, 0o666, '') assert 42 == fp.int('an_int', 0, 0o666, '')
&& true == fp.bool('a_bool', 0, false, '') assert true == fp.bool('a_bool', 0, false, '')
&& true == fp.bool('bool_without', 0, false, '') assert true == fp.bool('bool_without', 0, false, '')
&& 2.0 == fp.float('a_float', 0, 1.0, '') assert 2.0 == fp.float('a_float', 0, 1.0, '')
&& 'stuff' == fp.string('a_string', 0, 'not_stuff', '') assert 'stuff' == fp.string('a_string', 0, 'not_stuff', '')
} }
fn test_beaware_for_argument_names_with_same_prefix() { fn test_beaware_for_argument_names_with_same_prefix() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--short', '5', '--short',
'--shorter=7' '5',
'--shorter=7',
]) ])
assert 5 == fp.int('short', 0, 0o666, '') assert 5 == fp.int('short', 0, 0o666, '')
&& 7 == fp.int('shorter', 0, 0o666, '') assert 7 == fp.int('shorter', 0, 0o666, '')
} }
fn test_beaware_for_argument_names_with_same_prefix_inverse() { fn test_beaware_for_argument_names_with_same_prefix_inverse() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--shorter=7', '--shorter=7',
'--short', '5', '--short',
'5',
]) ])
assert 5 == fp.int('short', 0, 0o666, '') assert 5 == fp.int('short', 0, 0o666, '')
&& 7 == fp.int('shorter', 0, 0o666, '') assert 7 == fp.int('shorter', 0, 0o666, '')
} }
fn test_allow_to_skip_executable_path() { fn test_allow_to_skip_executable_path() {
mut fp := flag.new_flag_parser(['./path/to/execuable']) mut fp := flag.new_flag_parser(['./path/to/execuable'])
fp.skip_executable() fp.skip_executable()
args := fp.finalize() or { args := fp.finalize() or {
assert false assert false
return return
@ -106,37 +106,38 @@ fn test_allow_to_skip_executable_path() {
fn test_none_flag_arguments_are_allowed() { fn test_none_flag_arguments_are_allowed() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'file1', '--an_int=2', 'file2', 'file3', '--bool_without', 'file4', '--outfile', 'outfile']) 'file1',
'--an_int=2',
'file2',
'file3',
'--bool_without',
'file4',
'--outfile',
'outfile',
])
assert 2 == fp.int('an_int', 0, 0o666, '') assert 2 == fp.int('an_int', 0, 0o666, '')
&& 'outfile' == fp.string('outfile', 0, 'bad', '') assert 'outfile' == fp.string('outfile', 0, 'bad', '')
&& true == fp.bool('bool_without', 0, false, '') assert true == fp.bool('bool_without', 0, false, '')
} }
fn test_finalize_returns_none_flag_arguments_ordered() { fn test_finalize_returns_none_flag_arguments_ordered() {
mut fp := flag.new_flag_parser(['d', 'b', 'x', 'a', '--outfile', 'outfile']) mut fp := flag.new_flag_parser(['d', 'b', 'x', 'a', '--outfile', 'outfile'])
fp.string('outfile', 0, 'bad', '') fp.string('outfile', 0, 'bad', '')
finalized := fp.finalize() or { finalized := fp.finalize() or {
assert false assert false
return return
} }
expected := ['d', 'b', 'x', 'a'] expected := ['d', 'b', 'x', 'a']
mut all_as_expected := true
for i, v in finalized { for i, v in finalized {
all_as_expected = all_as_expected && v == expected[i] assert v == expected[i]
} }
assert all_as_expected
} }
fn test_finalize_returns_error_for_unknown_flags() { fn test_finalize_returns_error_for_unknown_flags() {
mut fp := flag.new_flag_parser(['--known', '--unknown']) mut fp := flag.new_flag_parser(['--known', '--unknown'])
fp.bool('known', 0, false, '') fp.bool('known', 0, false, '')
finalized := fp.finalize() or { finalized := fp.finalize() or {
assert err == 'Unknown argument \'unknown\'' assert err == "Unknown argument \'unknown\'"
return return
} }
assert finalized.len < 0 // expect error to be returned assert finalized.len < 0 // expect error to be returned
@ -148,27 +149,19 @@ fn test_allow_to_build_usage_message() {
fp.application('flag_tool') fp.application('flag_tool')
fp.version('v0.0.0') fp.version('v0.0.0')
fp.description('some short information about this tool') fp.description('some short information about this tool')
fp.int('an_int', 0, 0o666, 'some int to define') fp.int('an_int', 0, 0o666, 'some int to define')
fp.bool('a_bool', 0, false, 'some bool to define') fp.bool('a_bool', 0, false, 'some bool to define')
fp.bool('bool_without_but_really_big', 0, false, 'this should appear on the next line') fp.bool('bool_without_but_really_big', 0, false, 'this should appear on the next line')
fp.float('a_float', 0, 1.0, 'some float as well') fp.float('a_float', 0, 1.0, 'some float as well')
fp.string('a_string', 0, 'not_stuff', 'your credit card number') fp.string('a_string', 0, 'not_stuff', 'your credit card number')
usage := fp.usage() usage := fp.usage()
mut all_strings_found := true mut all_strings_found := true
for s in ['flag_tool', 'v0.0.0', for s in ['flag_tool', 'v0.0.0', 'an_int <int>', 'a_bool', 'bool_without', 'a_float <float>',
'an_int <int>', 'a_bool', 'bool_without', 'a_float <float>', 'a_string <string>', 'a_string <string>', 'some int to define', 'some bool to define', 'this should appear on the next line',
'some int to define', 'some float as well', 'your credit card number', 'The arguments should be at least 1 and at most 4 in number.',
'some bool to define', 'Usage', 'Options:', 'Description:', 'some short information about this tool'] {
'this should appear on the next line',
'some float as well',
'your credit card number',
'The arguments should be at least 1 and at most 4 in number.',
'Usage', 'Options:', 'Description:',
'some short information about this tool'] {
if !usage.contains(s) { if !usage.contains(s) {
eprintln(' missing \'$s\' in usage message') eprintln(" missing '$s' in usage message")
all_strings_found = false all_strings_found = false
} }
} }
@ -179,9 +172,7 @@ fn test_if_no_description_given_usage_message_does_not_contain_descpription() {
mut fp := flag.new_flag_parser([]) mut fp := flag.new_flag_parser([])
fp.application('flag_tool') fp.application('flag_tool')
fp.version('v0.0.0') fp.version('v0.0.0')
fp.bool('a_bool', 0, false, '') fp.bool('a_bool', 0, false, '')
assert !fp.usage().contains('Description:') assert !fp.usage().contains('Description:')
} }
@ -189,7 +180,6 @@ fn test_if_no_options_given_usage_message_does_not_contain_options() {
mut fp := flag.new_flag_parser([]) mut fp := flag.new_flag_parser([])
fp.application('flag_tool') fp.application('flag_tool')
fp.version('v0.0.0') fp.version('v0.0.0')
assert !fp.usage().contains('Options:') assert !fp.usage().contains('Options:')
} }
@ -200,7 +190,9 @@ fn test_free_args_could_be_limited() {
assert false assert false
return return
} }
assert args[0] == 'a' && args[1] == 'b' && args[2] == 'c' assert args[0] == 'a'
assert args[1] == 'b'
assert args[2] == 'c'
} }
fn test_error_for_to_few_free_args() { fn test_error_for_to_few_free_args() {
@ -235,16 +227,19 @@ fn test_could_expect_no_free_args() {
fn test_allow_abreviations() { fn test_allow_abreviations() {
mut fp := flag.new_flag_parser(['-v', '-o', 'some_file', '-i', '42', '-f', '2.0']) mut fp := flag.new_flag_parser(['-v', '-o', 'some_file', '-i', '42', '-f', '2.0'])
v := fp.bool('version', `v`, false, '') v := fp.bool('version', `v`, false, '')
o := fp.string('output', `o`, 'empty', '') o := fp.string('output', `o`, 'empty', '')
i := fp.int('count', `i`, 0, '') i := fp.int('count', `i`, 0, '')
f := fp.float('value', `f`, 0.0, '') f := fp.float('value', `f`, 0.0, '')
assert v == true
assert v && o == 'some_file' && i == 42 && f == 2.0 assert o == 'some_file'
assert i == 42
assert f == 2.0
u := fp.usage() u := fp.usage()
assert u.contains(' -v') && u.contains(' -o') && u.contains(' -i') && u.contains(' -f') assert u.contains(' -v')
assert u.contains(' -o')
assert u.contains(' -i')
assert u.contains(' -f')
assert u.contains(' -o, --output <string>') assert u.contains(' -o, --output <string>')
assert u.contains(' -i, --count <int>') assert u.contains(' -i, --count <int>')
assert u.contains(' -f, --value <float>') assert u.contains(' -f, --value <float>')
@ -253,15 +248,11 @@ fn test_allow_abreviations() {
fn test_allow_kebab_options() { fn test_allow_kebab_options() {
default_value := 'this_is_the_default_value_of_long_option' default_value := 'this_is_the_default_value_of_long_option'
long_option_value := 'this_is_a_long_option_value_as_argument' long_option_value := 'this_is_a_long_option_value_as_argument'
mut fp := flag.new_flag_parser(['--my-long-flag', 'true', '--my-long-option', long_option_value])
mut fp := flag.new_flag_parser(['--my-long-flag', 'true', '--my-long-option', long_option_value ])
my_flag := fp.bool('my-long-flag', 0, false, 'flag with long-kebab-name') my_flag := fp.bool('my-long-flag', 0, false, 'flag with long-kebab-name')
my_option := fp.string('my-long-option', 0, default_value, 'string with long-kebab-name') my_option := fp.string('my-long-option', 0, default_value, 'string with long-kebab-name')
assert my_flag == true assert my_flag == true
assert my_option == long_option_value assert my_option == long_option_value
u := fp.usage() u := fp.usage()
assert u.contains(' --my-long-flag') assert u.contains(' --my-long-flag')
assert u.contains(' --my-long-option') assert u.contains(' --my-long-option')
@ -273,7 +264,7 @@ fn test_not_provided_option_is_not_returned() {
fp.int_opt('some-flag', `a`, '') or { fp.int_opt('some-flag', `a`, '') or {
fp.float_opt('some-flag', `a`, '') or { fp.float_opt('some-flag', `a`, '') or {
fp.string_opt('some-flag', `a`, '') or { fp.string_opt('some-flag', `a`, '') or {
//Everything should not return // Everything should not return
return return
} }
return return
@ -282,54 +273,71 @@ fn test_not_provided_option_is_not_returned() {
} }
return return
} }
//If we reach here, one of them returned a value. // If we reach here, one of them returned a value.
assert false assert false
} }
fn test_provided_option_is_returned() { fn test_provided_option_is_returned() {
mut fp := flag.new_flag_parser(['-a', '-b', '3', '-c', 'hello', '-d', '3.14']) mut fp := flag.new_flag_parser(['-a', '-b', '3', '-c', 'hello', '-d', '3.14'])
a := fp.bool_opt('some-flag', `a`, '') or { a := fp.bool_opt('some-flag', `a`, '') or { panic('bool_opt did not return a bool') }
panic('bool_opt did not return a bool') b := fp.int_opt('some-flag', `b`, '') or { panic('int_opt did not return an int') }
} c := fp.string_opt('some-flag', `c`, '') or { panic('string_opt did not return a string') }
b := fp.int_opt('some-flag', `b`, '') or { d := fp.float_opt('some-flag', `d`, '') or { panic('float_opt did not return a float') }
panic('int_opt did not return an int') assert true == a
} assert b == 3
c := fp.string_opt('some-flag', `c`, '') or { assert c == 'hello'
panic('string_opt did not return a string') assert d == 3.14
}
d := fp.float_opt('some-flag', `d`, '') or {
panic('float_opt did not return a float')
}
assert a && b == 3 && c == 'hello' && d == 3.14
} }
fn test_multiple_arguments() { fn test_multiple_arguments() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'-a', '2', '-a', '3', '-a', '5', '-a',
'-b', 'a', '-b', 'c', '-b', 'b', '2',
'-c', '1.23', '-c', '2.34', '-c', '3.45' '-a',
'3',
'-a',
'5',
'-b',
'a',
'-b',
'c',
'-b',
'b',
'-c',
'1.23',
'-c',
'2.34',
'-c',
'3.45',
]) ])
// TODO Move to array comparison once it's implemented
//TODO Move to array comparison once it's implemented // assert fp.int_multi('some-flag', `a`, '') == [2, 3, 5] &&
//assert fp.int_multi('some-flag', `a`, '') == [2, 3, 5] &&
// fp.string_multi('some-flag', `b`, '') == ['a', 'c', 'b'] && // fp.string_multi('some-flag', `b`, '') == ['a', 'c', 'b'] &&
// fp.float_multi('some-flag', `c`, '') == [1.23, 2.34, 3.45] // fp.float_multi('some-flag', `c`, '') == [1.23, 2.34, 3.45]
a := fp.int_multi('some-flag', `a`, '') a := fp.int_multi('some-flag', `a`, '')
b := fp.string_multi('some-flag', `b`, '') b := fp.string_multi('some-flag', `b`, '')
c := fp.float_multi('some-flag', `c`, '') c := fp.float_multi('some-flag', `c`, '')
assert a.len == 3 && b.len == 3 && c.len == 3 assert a.len == 3
assert a[0] == 2 && a[1] == 3 && a[2] == 5 assert b.len == 3
assert b[0] == 'a' && b[1] == 'c' && b[2] == 'b' assert c.len == 3
assert c[0] == 1.23 && c[1] == 2.34 && c[2] == 3.45 assert a[0] == 2
assert a[1] == 3
assert a[2] == 5
assert b[0] == 'a'
assert b[1] == 'c'
assert b[2] == 'b'
assert c[0] == 1.23
assert c[1] == 2.34
assert c[2] == 3.45
} }
fn test_long_options_that_start_with_the_same_letter_as_another_short_option() { fn test_long_options_that_start_with_the_same_letter_as_another_short_option() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'--vabc', '/abc', '--vabc',
'/abc',
]) ])
verbose := fp.bool('verbose', `v`, false, 'Be more verbose.') verbose := fp.bool('verbose', `v`, false, 'Be more verbose.')
vabc := fp.string('vabc', `x`, 'default', 'Another option that *may* conflict with v, but *should not*') vabc := fp.string('vabc', `x`, 'default', 'Another option that *may* conflict with v, but *should not*')
assert verbose == false assert verbose == false
assert vabc == '/abc' assert vabc == '/abc'
} }
@ -337,17 +345,18 @@ fn test_long_options_that_start_with_the_same_letter_as_another_short_option() {
fn test_long_options_that_start_with_the_same_letter_as_another_short_option_both_set() { fn test_long_options_that_start_with_the_same_letter_as_another_short_option_both_set() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'-v', '-v',
'--vabc', '/abc', '--vabc',
'/abc',
]) ])
verbose := fp.bool('verbose', `v`, false, 'Be more verbose.') verbose := fp.bool('verbose', `v`, false, 'Be more verbose.')
vabc := fp.string('vabc', `x`, 'default', 'Another option that *may* conflict with v, but *should not*') vabc := fp.string('vabc', `x`, 'default', 'Another option that *may* conflict with v, but *should not*')
assert verbose == true assert verbose == true
assert vabc == '/abc' assert vabc == '/abc'
} }
fn test_single_dash() { fn test_single_dash() {
mut fp := flag.new_flag_parser([ mut fp := flag.new_flag_parser([
'-' '-',
]) ])
flag_update := fp.bool('update', `u`, false, 'Update tools') flag_update := fp.bool('update', `u`, false, 'Update tools')
assert flag_update == false assert flag_update == false