Add string collect

The `string collect` subcommand behaves quite similarly in practice to
`string split0 -m 0` in that it doesn't split its output, but it also
takes an optional `--trim-newline` flag to trim a single trailing
newline off of the output.

See issue #159.
This commit is contained in:
Lily Ballard
2019-06-15 22:30:31 -07:00
parent 5fd3bf79f5
commit b41e5cbbb7
7 changed files with 132 additions and 4 deletions

View File

@@ -6,6 +6,8 @@ string - manipulate strings
Synopsis
--------
``string collect [(-n | --trim-newline)] [STRING...]``
``string escape [(-n | --no-quoted)] [--style=xxx] [STRING...]``
``string join [(-q | --quiet)] SEP [STRING...]``
@@ -48,6 +50,17 @@ Most subcommands accept a ``-q`` or ``--quiet`` switch, which suppresses the usu
The following subcommands are available.
"collect" subcommand
--------------------
``string collect [(-n | --trim-newline)] [STRING...]``
``string collect`` collects its input into a single output argument, without splitting the output when used in a command substitution. This is useful when trying to collect multiline output from another command into a variable. Exit status: 0 if any output argument is non-empty, or 1 otherwise.
If invoked with multiple arguments instead of input, ``string collect`` preserves each argument separately, where the number of output arguments is equal to the number of arguments given to ``string collect``.
``--trim-newline`` trims a single trailing newline off of each output argument. This is useful when collecting the output from another command as the trailing newline is frequently not desired.
"escape" and "unescape" subcommands
-----------------------------------
@@ -329,6 +342,22 @@ Examples
a1_20b2__c_E6_85_A1
::
>_ echo \"(echo one\ntwo\nthree | string collect)\"
"one
two
three
"
>_ echo \"(ech one\ntwo\nthree | string collect -n)\"
"one
two
three"
Match Glob Examples
-------------------