mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
flag: switch panics to optionals (#11515)
This commit is contained in:
parent
0a18690a4f
commit
1688148828
@ -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).')
|
||||
|
@ -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:
|
||||
|
@ -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.')
|
||||
|
@ -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)
|
||||
|
@ -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.')
|
||||
|
@ -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')
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user