string: Allow collect --allow-empty to avoid empty ellision (#8054)

* string: Allow `collect --no-empty` to avoid empty ellision

Currently we still have that issue where

    test -n (thing | string collect)

can return true if `thing` doesn't print anything, because the
collected argument will still be removed.

So, what we do is allow `--no-empty` to be used, in which case we
print one empty argument.

This means

    test -n (thing | string collect -n)

can now be safely used.

"no-empty" isn't the best name for this flag, but string's design
really incentivizes reusing names, and it's not *terrible*.

* Switch to `--allow-empty`

`--no-empty` does the exact opposite for `string split` and split0.

Since `-a`/`--allow-empty` already exists, use it.
This commit is contained in:
Fabian Homborg
2021-07-09 21:20:58 +02:00
committed by GitHub
parent 32826d3596
commit 0e1f5108ae
3 changed files with 27 additions and 1 deletions

View File

@@ -667,6 +667,18 @@ string collect -N '' >/dev/null; and echo unexpected success; or echo expected f
string collect \n\n >/dev/null; and echo unexpected success; or echo expected failure
# CHECK: expected failure
echo "foo"(true | string collect --allow-empty)"bar"
# CHECK: foobar
test -z (string collect)
and echo Nothing
# CHECK: Nothing
test -n (string collect)
and echo Something
# CHECK: Something
test -n (string collect -a)
or echo No, actually nothing
# CHECK: No, actually nothing
# string collect in functions
# This function outputs some newline-separated content, and some
# explicitly un-separated content.