flag: switch panics to optionals (#11515)

pull/11519/head
JalonSolov 2021-09-16 12:25:05 -04:00 committed by GitHub
parent 0a18690a4f
commit 1688148828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 26 additions and 26 deletions

View File

@ -123,7 +123,7 @@ fn main() {
} }
should_sync := fp.bool('cache-sync', `s`, false, 'Update the local cache') should_sync := fp.bool('cache-sync', `s`, false, 'Update the local cache')
if !should_sync { 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).') context.cleanup = fp.bool('clean', 0, false, 'Clean before running (slower).')

View File

@ -192,7 +192,7 @@ fn main() {
fp.description(tool_description) fp.description(tool_description)
fp.arguments_description('COMMIT_BEFORE [COMMIT_AFTER]') fp.arguments_description('COMMIT_BEFORE [COMMIT_AFTER]')
fp.skip_executable() 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.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. context.hyperfineopts = fp.string('hyperfine_options', 0, '', 'Additional options passed to hyperfine.
${flag.space}For example on linux, you may want to pass: ${flag.space}For example on linux, you may want to pass:

View File

@ -143,19 +143,19 @@ const (
fn main() { fn main() {
mut context := Context{} mut context := Context{}
context.parse_options() context.parse_options() ?
context.run() context.run()
context.show_diff_summary() context.show_diff_summary()
} }
fn (mut context Context) parse_options() { fn (mut context Context) parse_options() ? {
mut fp := flag.new_flag_parser(os.args) mut fp := flag.new_flag_parser(os.args)
fp.application(os.file_name(os.executable())) fp.application(os.file_name(os.executable()))
fp.version('0.0.1') 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.description('Repeat command(s) and collect statistics. NB: you have to quote each command, if it contains spaces.')
fp.arguments_description('CMD1 CMD2 ...') fp.arguments_description('CMD1 CMD2 ...')
fp.skip_executable() 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.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.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.') context.warmup = fp.int('warmup', `w`, 2, 'Warmup runs. These are done *only at the start*, and are ignored.')

View File

@ -46,7 +46,7 @@ fn main() {
for hf in hfields.split(',') { for hf in hfields.split(',') {
mhf.names[hf] = true 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() rest_of_args := fp.remaining_parameters()
for vfile in rest_of_args { for vfile in rest_of_args {
file := get_abs_path(vfile) file := get_abs_path(vfile)

View File

@ -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.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.arguments_description('[--silent] [--clear] [--ignore .db] [--add /path/to/a/file.v] [run] program.v')
fp.allow_unknown_args() 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.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.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.') context.clear_terminal = fp.bool('clear', `c`, false, 'Clears the terminal before each re-run.')

View File

@ -17,7 +17,7 @@ fn main() {
mut fp := flag.new_flag_parser(os.args) mut fp := flag.new_flag_parser(os.args)
fp.application('flag_example_tool') fp.application('flag_example_tool')
fp.version('v0.0.1') 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.description('This tool is only designed to show how the flag lib is working')
fp.skip_executable() fp.skip_executable()
an_int := fp.int('an_int', 0, 0o123, 'some int to define 0o123 is its default value') an_int := fp.int('an_int', 0, 0o123, 'some int to define 0o123 is its default value')

View File

@ -429,22 +429,22 @@ pub fn (mut fs FlagParser) string(name string, abbr byte, sdefault string, usage
return value 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 { 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 { 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 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 { 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 { 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.min_free_args = n
fs.max_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 // this will cause an error in finalize() if free args are out of range
// (min, ..., max) // (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 { 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.min_free_args = min
fs.max_free_args = max fs.max_free_args = max

View File

@ -153,9 +153,9 @@ fn test_finalize_returns_error_for_unknown_flags_short() {
assert finalized.len < 0 // expect error to be returned 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([]) mut fp := flag.new_flag_parser([])
fp.limit_free_args(1, 4) fp.limit_free_args(1, 4) ?
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')
@ -194,9 +194,9 @@ fn test_if_no_options_given_usage_message_does_not_contain_options() {
assert !fp.usage().contains('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']) 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 { args := fp1.finalize() or {
assert false assert false
return return
@ -206,9 +206,9 @@ fn test_free_args_could_be_limited() {
assert args[2] == 'c' 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']) 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 { args := fp1.finalize() or {
assert err.msg.starts_with('Expected at least 5 arguments') assert err.msg.starts_with('Expected at least 5 arguments')
return 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 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']) 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 { args := fp1.finalize() or {
assert err.msg.starts_with('Expected at most 2 arguments') assert err.msg.starts_with('Expected at most 2 arguments')
return 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 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']) mut fp1 := flag.new_flag_parser(['a'])
fp1.limit_free_args(0, 0) fp1.limit_free_args(0, 0) ?
args := fp1.finalize() or { args := fp1.finalize() or {
assert err.msg.starts_with('Expected no arguments') assert err.msg.starts_with('Expected no arguments')
return return