mirror of
https://github.com/nushell/nushell
synced 2025-01-14 06:04:09 +00:00
enable to pass switch values dynamically (#11057)
# Description Closes: #7260 About the change: When we make an internalcall, and meet a `switch` (Flag.arg is None), nushell will try to see if the switch is called like `--xyz=false` , if that is true, `parse_long_flag` will return relative value. # User-Facing Changes So after the pr, the following would be possible: ```nushell def build-imp [--push, --release] { echo $"Doing a build with push: ($push) and release: ($release)" } def build [--push, --release] { build-imp --push=$push --release=$release } build --push --release=false build --push=false --release=true build --push=false --release=false build --push --release build ``` # Tests + Formatting Done # After Submitting Needs to submit a doc update, mentioned about the difference between `def a [--x] {}` and `def a [--x: bool] {}`
This commit is contained in:
parent
b2734db015
commit
6cfe35eb7e
2 changed files with 55 additions and 8 deletions
|
@ -401,13 +401,31 @@ fn parse_long_flag(
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// A flag with no argument
|
// A flag with no argument
|
||||||
(
|
// It can also takes a boolean value like --x=true
|
||||||
Some(Spanned {
|
if split.len() > 1 {
|
||||||
item: long_name,
|
// and we also have the argument
|
||||||
span: arg_span,
|
let long_name_len = long_name.len();
|
||||||
}),
|
let mut span = arg_span;
|
||||||
None,
|
span.start += long_name_len + 3; //offset by long flag and '='
|
||||||
)
|
|
||||||
|
let arg = parse_value(working_set, span, &SyntaxShape::Boolean);
|
||||||
|
|
||||||
|
(
|
||||||
|
Some(Spanned {
|
||||||
|
item: long_name,
|
||||||
|
span: Span::new(arg_span.start, arg_span.start + long_name_len + 2),
|
||||||
|
}),
|
||||||
|
Some(arg),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
Some(Spanned {
|
||||||
|
item: long_name,
|
||||||
|
span: arg_span,
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
working_set.error(ParseError::UnknownFlag(
|
working_set.error(ParseError::UnknownFlag(
|
||||||
|
|
|
@ -83,6 +83,35 @@ fn custom_switch2() -> TestResult {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn custom_switch3() -> TestResult {
|
fn custom_switch3() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
r#"def florb [ --dry-run ] { $dry_run }; florb --dry-run=false"#,
|
||||||
|
"false",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn custom_switch4() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
r#"def florb [ --dry-run ] { $dry_run }; florb --dry-run=true"#,
|
||||||
|
"true",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn custom_switch5() -> TestResult {
|
||||||
|
run_test(r#"def florb [ --dry-run ] { $dry_run }; florb"#, "false")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn custom_switch6() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
r#"def florb [ --dry-run ] { $dry_run }; florb --dry-run"#,
|
||||||
|
"true",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn custom_flag1() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"def florb [
|
r#"def florb [
|
||||||
--age: int = 0
|
--age: int = 0
|
||||||
|
@ -96,7 +125,7 @@ fn custom_switch3() -> TestResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn custom_switch4() -> TestResult {
|
fn custom_flag2() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"def florb [
|
r#"def florb [
|
||||||
--age: int
|
--age: int
|
||||||
|
|
Loading…
Reference in a new issue