Optional syntax for multiple arguments as well as for one optional argument
remimimimimi opened this issue · comments
Rémi commented
Bool then
function suits pretty good in terms of optional arguments, but current cmd!
do not support Option
with multiple arguments.
Having optional syntax available for multiple arguments as well as for one argument gives cleaner code. So instead of writing every time if condition { ... } else { ... }
for several arguments we can use Option
either for one argument and many arguments.
With that feature this code from documentation
let check = if true { &["--", "--check"] } else { &[][..] };
assert_eq!(
cmd!("cargo fmt {check...}").to_string(),
"cargo fmt -- --check"
);
let dry_run = if true { Some("--dry-run") } else { None };
assert_eq!(
cmd!("git push {dry_run...}").to_string(),
"git push --dry-run"
);
Can be rewritten as
let condition = true;
let check = condition.then(|| &["--", "--check"]);
assert_eq!(
cmd!("cargo fmt {check...}").to_string(),
"cargo fmt -- --check"
);
let dry_run = condition.then(|| "--dry-run");
assert_eq!(
cmd!("git push {dry_run...}").to_string(),
"git push --dry-run"
);
Alex Kladov commented
The second case, condition.then(|| "--dry-run")
already works. The first case requires condition.then(|| &["--", "--check"]).into_iter().flatten()
.