From 1688148828ac0eaf88075f3877b90886c9b0deb4 Mon Sep 17 00:00:00 2001 From: JalonSolov Date: Thu, 16 Sep 2021 12:25:05 -0400 Subject: [PATCH] flag: switch panics to optionals (#11515) --- cmd/tools/oldv.v | 2 +- cmd/tools/performance_compare.v | 2 +- cmd/tools/repeat.v | 6 +++--- cmd/tools/vast/vast.v | 2 +- cmd/tools/vwatch.v | 2 +- vlib/flag/README.md | 2 +- vlib/flag/flag.v | 16 ++++++++-------- vlib/flag/flag_test.v | 20 ++++++++++---------- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/cmd/tools/oldv.v b/cmd/tools/oldv.v index 03eaf83ef5..08b3083e91 100644 --- a/cmd/tools/oldv.v +++ b/cmd/tools/oldv.v @@ -123,7 +123,7 @@ fn main() { } should_sync := fp.bool('cache-sync', `s`, false, 'Update the local cache') if !should_sync { - fp.limit_free_args(1, 1) + fp.limit_free_args(1, 1) ? } //// context.cleanup = fp.bool('clean', 0, false, 'Clean before running (slower).') diff --git a/cmd/tools/performance_compare.v b/cmd/tools/performance_compare.v index 6723231524..648defd907 100644 --- a/cmd/tools/performance_compare.v +++ b/cmd/tools/performance_compare.v @@ -192,7 +192,7 @@ fn main() { fp.description(tool_description) fp.arguments_description('COMMIT_BEFORE [COMMIT_AFTER]') fp.skip_executable() - fp.limit_free_args(1, 2) + fp.limit_free_args(1, 2) ? context.vflags = fp.string('vflags', 0, '', 'Additional options to pass to the v commands, for example "-cc tcc"') context.hyperfineopts = fp.string('hyperfine_options', 0, '', 'Additional options passed to hyperfine. ${flag.space}For example on linux, you may want to pass: diff --git a/cmd/tools/repeat.v b/cmd/tools/repeat.v index e69cfdfde7..8adc565d92 100644 --- a/cmd/tools/repeat.v +++ b/cmd/tools/repeat.v @@ -143,19 +143,19 @@ const ( fn main() { mut context := Context{} - context.parse_options() + context.parse_options() ? context.run() context.show_diff_summary() } -fn (mut context Context) parse_options() { +fn (mut context Context) parse_options() ? { mut fp := flag.new_flag_parser(os.args) fp.application(os.file_name(os.executable())) fp.version('0.0.1') fp.description('Repeat command(s) and collect statistics. NB: you have to quote each command, if it contains spaces.') fp.arguments_description('CMD1 CMD2 ...') fp.skip_executable() - fp.limit_free_args_to_at_least(1) + fp.limit_free_args_to_at_least(1) ? context.count = fp.int('count', `c`, 10, 'Repetition count.') context.series = fp.int('series', `s`, 2, 'Series count. `-s 2 -c 4 a b` => aaaabbbbaaaabbbb, while `-s 3 -c 2 a b` => aabbaabbaabb.') context.warmup = fp.int('warmup', `w`, 2, 'Warmup runs. These are done *only at the start*, and are ignored.') diff --git a/cmd/tools/vast/vast.v b/cmd/tools/vast/vast.v index 8a6c008dd2..b037d1f6b7 100644 --- a/cmd/tools/vast/vast.v +++ b/cmd/tools/vast/vast.v @@ -46,7 +46,7 @@ fn main() { for hf in hfields.split(',') { mhf.names[hf] = true } - fp.limit_free_args_to_at_least(1) + fp.limit_free_args_to_at_least(1) ? rest_of_args := fp.remaining_parameters() for vfile in rest_of_args { file := get_abs_path(vfile) diff --git a/cmd/tools/vwatch.v b/cmd/tools/vwatch.v index 0ac243d7ab..e718ea5a51 100644 --- a/cmd/tools/vwatch.v +++ b/cmd/tools/vwatch.v @@ -314,7 +314,7 @@ fn main() { fp.description('Collect all .v files needed for a compilation, then re-run the compilation when any of the source changes.') fp.arguments_description('[--silent] [--clear] [--ignore .db] [--add /path/to/a/file.v] [run] program.v') fp.allow_unknown_args() - fp.limit_free_args_to_at_least(1) + fp.limit_free_args_to_at_least(1) ? context.is_worker = fp.bool('vwatchworker', 0, false, 'Internal flag. Used to distinguish vwatch manager and worker processes.') context.silent = fp.bool('silent', `s`, false, 'Be more silent; do not print the watch timestamp before each re-run.') context.clear_terminal = fp.bool('clear', `c`, false, 'Clears the terminal before each re-run.') diff --git a/vlib/flag/README.md b/vlib/flag/README.md index 1122f77f55..87c94e604e 100644 --- a/vlib/flag/README.md +++ b/vlib/flag/README.md @@ -17,7 +17,7 @@ fn main() { mut fp := flag.new_flag_parser(os.args) fp.application('flag_example_tool') fp.version('v0.0.1') - fp.limit_free_args(0, 0) // comment this, if you expect arbitrary texts after the options + fp.limit_free_args(0, 0) ? // comment this, if you expect arbitrary texts after the options fp.description('This tool is only designed to show how the flag lib is working') fp.skip_executable() an_int := fp.int('an_int', 0, 0o123, 'some int to define 0o123 is its default value') diff --git a/vlib/flag/flag.v b/vlib/flag/flag.v index 85bb09de7f..21f4731d87 100644 --- a/vlib/flag/flag.v +++ b/vlib/flag/flag.v @@ -429,22 +429,22 @@ pub fn (mut fs FlagParser) string(name string, abbr byte, sdefault string, usage return value } -pub fn (mut fs FlagParser) limit_free_args_to_at_least(n int) { +pub fn (mut fs FlagParser) limit_free_args_to_at_least(n int) ? { if n > flag.max_args_number { - panic('flag.limit_free_args_to_at_least expect n to be smaller than $flag.max_args_number') + return error('flag.limit_free_args_to_at_least expect n to be smaller than $flag.max_args_number') } if n <= 0 { - panic('flag.limit_free_args_to_at_least expect n to be a positive number') + return error('flag.limit_free_args_to_at_least expect n to be a positive number') } fs.min_free_args = n } -pub fn (mut fs FlagParser) limit_free_args_to_exactly(n int) { +pub fn (mut fs FlagParser) limit_free_args_to_exactly(n int) ? { if n > flag.max_args_number { - panic('flag.limit_free_args_to_exactly expect n to be smaller than $flag.max_args_number') + return error('flag.limit_free_args_to_exactly expect n to be smaller than $flag.max_args_number') } if n < 0 { - panic('flag.limit_free_args_to_exactly expect n to be a non negative number') + return error('flag.limit_free_args_to_exactly expect n to be a non negative number') } fs.min_free_args = n fs.max_free_args = n @@ -452,9 +452,9 @@ pub fn (mut fs FlagParser) limit_free_args_to_exactly(n int) { // this will cause an error in finalize() if free args are out of range // (min, ..., max) -pub fn (mut fs FlagParser) limit_free_args(min int, max int) { +pub fn (mut fs FlagParser) limit_free_args(min int, max int) ? { if min > max { - panic('flag.limit_free_args expect min < max, got $min >= $max') + return error('flag.limit_free_args expect min < max, got $min >= $max') } fs.min_free_args = min fs.max_free_args = max diff --git a/vlib/flag/flag_test.v b/vlib/flag/flag_test.v index 83261932b2..be52e9788b 100644 --- a/vlib/flag/flag_test.v +++ b/vlib/flag/flag_test.v @@ -153,9 +153,9 @@ fn test_finalize_returns_error_for_unknown_flags_short() { assert finalized.len < 0 // expect error to be returned } -fn test_allow_to_build_usage_message() { +fn test_allow_to_build_usage_message() ? { mut fp := flag.new_flag_parser([]) - fp.limit_free_args(1, 4) + fp.limit_free_args(1, 4) ? fp.application('flag_tool') fp.version('v0.0.0') fp.description('some short information about this tool') @@ -194,9 +194,9 @@ fn test_if_no_options_given_usage_message_does_not_contain_options() { assert !fp.usage().contains('Options:') } -fn test_free_args_could_be_limited() { +fn test_free_args_could_be_limited() ? { mut fp1 := flag.new_flag_parser(['a', 'b', 'c']) - fp1.limit_free_args(1, 4) + fp1.limit_free_args(1, 4) ? args := fp1.finalize() or { assert false return @@ -206,9 +206,9 @@ fn test_free_args_could_be_limited() { assert args[2] == 'c' } -fn test_error_for_to_few_free_args() { +fn test_error_for_to_few_free_args() ? { mut fp1 := flag.new_flag_parser(['a', 'b', 'c']) - fp1.limit_free_args(5, 6) + fp1.limit_free_args(5, 6) ? args := fp1.finalize() or { assert err.msg.starts_with('Expected at least 5 arguments') return @@ -216,9 +216,9 @@ fn test_error_for_to_few_free_args() { assert args.len < 0 // expect an error and need to use args } -fn test_error_for_to_much_free_args() { +fn test_error_for_to_much_free_args() ? { mut fp1 := flag.new_flag_parser(['a', 'b', 'c']) - fp1.limit_free_args(1, 2) + fp1.limit_free_args(1, 2) ? args := fp1.finalize() or { assert err.msg.starts_with('Expected at most 2 arguments') return @@ -226,9 +226,9 @@ fn test_error_for_to_much_free_args() { assert args.len < 0 // expect an error and need to use args } -fn test_could_expect_no_free_args() { +fn test_could_expect_no_free_args() ? { mut fp1 := flag.new_flag_parser(['a']) - fp1.limit_free_args(0, 0) + fp1.limit_free_args(0, 0) ? args := fp1.finalize() or { assert err.msg.starts_with('Expected no arguments') return