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

@@ -8,7 +8,7 @@ Synopsis
::
string collect [(-N | --no-trim-newlines)] [STRING...]
string collect [(-a | --allow-empty)] [(-N | --no-trim-newlines)] [STRING...]
.. END SYNOPSIS
@@ -23,6 +23,8 @@ If invoked with multiple arguments instead of input, ``string collect`` preserve
Any trailing newlines on the input are trimmed, just as with ``"$(cmd)"`` substitution in sh. ``--no-trim-newlines`` can be used to disable this behavior, which may be useful when running a command such as ``set contents (cat filename | string collect -N)``.
With ``--allow-empty``, ``string collect`` always prints one (empty) argument. This can be used to prevent an argument from disappearing.
.. END DESCRIPTION
Examples
@@ -43,4 +45,7 @@ Examples
three
"
>_ echo foo(true | string collect --allow-empty)bar
foobar
.. END EXAMPLES