Return 1 if non-existent field is given

This commit is contained in:
Jason Nader 2020-03-24 23:25:37 +09:00 committed by Fabian Homborg
parent 1329a40e87
commit 21bbd2ecb4
2 changed files with 7 additions and 4 deletions

View file

@ -1175,10 +1175,13 @@ static int string_split_maybe0(parser_t &parser, io_streams_t &streams, int argc
if (opts.fields.size() > 0) {
for (const auto &field : opts.fields) {
// field indexing starts from 1
if (field - 1 < (long)split_count) {
buff.append(splits.at(field - 1), separation_type_t::explicitly);
if (field - 1 >= (long)split_count) {
return STATUS_CMD_ERROR;
}
}
for (const auto &field : opts.fields) {
buff.append(splits.at(field - 1), separation_type_t::explicitly);
}
} else {
for (const wcstring &split : splits) {
buff.append(split, separation_type_t::explicitly);

View file

@ -95,8 +95,8 @@ string split --fields=3,2 "" abc
# CHECK: c
# CHECK: b
string split --fields=2,9 "" abc
# CHECK: b
string split --fields=2,9 "" abc; or echo "exit 1"
# CHECK: exit 1
string split --fields=1-3,5,9-7 "" 123456789
# CHECK: 1