mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-22 15:18:50 +08:00
Allow switch
with something that expands to nothing
Meaning empty variables, command substitutions that don't print anything. A switch without an argument ```fish switch case ... end ``` is still a syntax error, and more than one argument is still a runtime error. The none-argument matches either an empty-string `case ''` or a catch-all `case '*'`. Fixes #5677. Fixes #4943.
This commit is contained in:
parent
57ffd18787
commit
6500765256
|
@ -20,6 +20,7 @@
|
|||
- Local values for `fish_complete_path` and `fish_function_path` are now ignored; only their global values are respected.
|
||||
- Empty universal variables may now be exported (#5992).
|
||||
- `string split` had a bug where empty strings would be dropped if the output was only empty strings; this has been fixed (#5987).
|
||||
- `switch` now allows arguments that expand to nothing, like empty variables (#5677).
|
||||
|
||||
### Syntax changes and new commands
|
||||
- Brace expansion now only takes place if the braces include a "," or a variable expansion, so things like `git reset HEAD@{0}` now work (#5869).
|
||||
|
|
|
@ -478,9 +478,9 @@ parse_execution_result_t parse_execution_context_t::run_switch_statement(
|
|||
}
|
||||
}
|
||||
|
||||
if (result == parse_execution_success && switch_values_expanded.size() != 1) {
|
||||
if (result == parse_execution_success && switch_values_expanded.size() > 1) {
|
||||
result =
|
||||
report_error(switch_value_n, _(L"switch: Expected exactly one argument, got %lu\n"),
|
||||
report_error(switch_value_n, _(L"switch: Expected at most one argument, got %lu\n"),
|
||||
switch_values_expanded.size());
|
||||
}
|
||||
|
||||
|
@ -488,7 +488,7 @@ parse_execution_result_t parse_execution_context_t::run_switch_statement(
|
|||
return result;
|
||||
}
|
||||
|
||||
const wcstring &switch_value_expanded = switch_values_expanded.at(0).completion;
|
||||
const wcstring &switch_value_expanded = switch_values_expanded.size() == 1 ? switch_values_expanded.at(0).completion : L"";
|
||||
|
||||
block_t *sb = parser->push_block(block_t::switch_block());
|
||||
|
||||
|
|
71
tests/checks/switch.fish
Normal file
71
tests/checks/switch.fish
Normal file
|
@ -0,0 +1,71 @@
|
|||
#RUN: %fish -C "set fish %fish" %s
|
||||
# Check that switch with an argument expanding to nothing still works.
|
||||
switch $foo
|
||||
case a
|
||||
echo a
|
||||
case ''
|
||||
echo very little
|
||||
case '*'
|
||||
echo Nothin
|
||||
case ''
|
||||
echo banana
|
||||
end
|
||||
#CHECK: very little
|
||||
|
||||
switch (true)
|
||||
case a
|
||||
echo a
|
||||
case ''
|
||||
echo very little
|
||||
case '*'
|
||||
echo Nothin
|
||||
case ''
|
||||
echo banana
|
||||
end
|
||||
#CHECK: very little
|
||||
|
||||
switch (echo)
|
||||
case a
|
||||
echo a
|
||||
case ''
|
||||
echo very little
|
||||
case '*'
|
||||
echo Nothin
|
||||
case ''
|
||||
echo banana
|
||||
end
|
||||
#CHECK: very little
|
||||
|
||||
# Multiple arguments is still an error, we've not decided on good behavior for this.
|
||||
switch (echo; echo; echo)
|
||||
case a
|
||||
echo a
|
||||
case ""
|
||||
echo very little
|
||||
case "*"
|
||||
echo Nothin
|
||||
case ""
|
||||
echo banana
|
||||
end
|
||||
#CHECKERR: {{.*/?}}switch.fish (line {{\d+}}): switch: Expected at most one argument, got 3
|
||||
#CHECKERR:
|
||||
#CHECKERR: switch (echo; echo; echo)
|
||||
#CHECKERR: ^
|
||||
|
||||
# As is no argument at all.
|
||||
# Because this is a syntax error, we need to start a sub-fish or we wouldn't execute anything else.
|
||||
$fish -c '
|
||||
switch
|
||||
case a
|
||||
echo a
|
||||
case ""
|
||||
echo very little
|
||||
case "*"
|
||||
echo Nothin
|
||||
case ""
|
||||
echo banana
|
||||
end
|
||||
'
|
||||
#CHECKERR: fish: 'case' builtin not inside of switch block
|
||||
#CHECKERR: case a
|
||||
#CHECKERR: ^
|
Loading…
Reference in New Issue
Block a user