parser: check if guard condition (#13765)
parent
54b0a2aa62
commit
bb2ddb98a3
|
@ -107,6 +107,10 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
|
|||
p.check(.decl_assign)
|
||||
comments << p.eat_comments()
|
||||
expr := p.expr(0)
|
||||
if expr !in [ast.CallExpr, ast.IndexExpr, ast.PrefixExpr] {
|
||||
p.error_with_pos('if guard condition expression is illegal, it should return optional',
|
||||
expr.pos())
|
||||
}
|
||||
|
||||
cond = ast.IfGuardExpr{
|
||||
vars: vars
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
vlib/v/parser/tests/if_guard_cond_err.vv:16:16: error: if guard condition expression is illegal, it should return optional
|
||||
14 | fp.usage_example('GOOG AAPL')
|
||||
15 | _ := fp.bool('version', `v`, false, 'version information.')
|
||||
16 | if args := fp.finalize() && args.len > 0 {
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
17 | return args
|
||||
18 | } else {
|
|
@ -0,0 +1,28 @@
|
|||
import os
|
||||
import flag
|
||||
|
||||
const version = "v0.1.0"
|
||||
|
||||
// getting command line options and arguments
|
||||
// returns the arguments
|
||||
fn get_args() ?[]string {
|
||||
mut fp := flag.new_flag_parser(os.args)
|
||||
fp.application('ticker')
|
||||
fp.version(version)
|
||||
fp.description('A CLI yahoo ticker app')
|
||||
fp.skip_executable()
|
||||
fp.usage_example('GOOG AAPL')
|
||||
_ := fp.bool('version', `v`, false, 'version information.')
|
||||
if args := fp.finalize() && args.len > 0 {
|
||||
return args
|
||||
} else {
|
||||
eprintln(err.msg())
|
||||
println(fp.usage())
|
||||
return none
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tickers := get_args() or { return }
|
||||
println(tickers)
|
||||
}
|
Loading…
Reference in New Issue