From 48c75831fc594fb3830863aa57347e423122379b Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour <51247512+Mehrbod2002@users.noreply.github.com> Date: Wed, 26 Apr 2023 16:46:32 +0330 Subject: [PATCH] Flags and args on def (#8953) # Description Fixes #8916 Fix flags and args on def which were call wrong . Added some tests too . --- crates/nu-engine/src/eval.rs | 17 ++++++++++++++++- src/tests/test_engine.rs | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index d4a09f68d..7fa74c782 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -112,7 +112,22 @@ pub fn eval_call( if let Some(var_id) = named.var_id { let mut found = false; for call_named in call.named_iter() { - if call_named.0.item == named.long { + if let (Some(spanned), Some(short)) = (&call_named.1, named.short) { + if spanned.item == short.to_string() { + if let Some(arg) = &call_named.2 { + let result = eval_expression(engine_state, caller_stack, arg)?; + + callee_stack.add_var(var_id, result); + } else if let Some(arg) = &named.default_value { + let result = eval_expression(engine_state, caller_stack, arg)?; + + callee_stack.add_var(var_id, result); + } else { + callee_stack.add_var(var_id, Value::boolean(true, call.head)) + } + found = true; + } + } else if call_named.0.item == named.long { if let Some(arg) = &call_named.2 { let result = eval_expression(engine_state, caller_stack, arg)?; diff --git a/src/tests/test_engine.rs b/src/tests/test_engine.rs index e0d6e980b..0de39f491 100644 --- a/src/tests/test_engine.rs +++ b/src/tests/test_engine.rs @@ -379,3 +379,27 @@ fn assignment_to_in_var_no_panic() -> TestResult { fn assignment_to_env_no_panic() -> TestResult { fail_test(r#"$env = 3"#, "cannot_replace_env") } + +#[test] +fn short_flags() -> TestResult { + run_test( + r#"def foobar [-a: int, -b: string, -c: string] { echo $'($a) ($c) ($b)' }; foobar -b "balh balh" -a 1543 -c "FALSE123""#, + "1543 FALSE123 balh balh", + ) +} + +#[test] +fn short_flags_1() -> TestResult { + run_test( + r#"def foobar [-a: string, -b: string, -s: int] { if ( $s == 0 ) { echo $'($b)($a)' }}; foobar -a test -b case -s 0 "#, + "casetest", + ) +} + +#[test] +fn short_flags_2() -> TestResult { + run_test( + r#"def foobar [-a: int, -b: string, -c: int] { $a + $c };foobar -b "balh balh" -a 10 -c 1 "#, + "11", + ) +}