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:
@@ -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
|
||||
|
Reference in New Issue
Block a user