module watt.process.cmd
Functions for parsing a command string.
These don't actually run any commands, but are useful for dealing with functions that do run a command string.
Code Map
//! Functions for parsing a command string.
module watt.process.cmd;
//! Surround the string with " and escape " and /.
fn escapeAndAddQuotation(sink: scope (Sink), str: scope (SinkArg)) { }
//! Returns a textual representation of cmd and args that can be passed to
//! "/bin/sh -c".
fn toArgsPosix(cmd: scope (SinkArg), args: scope (SinkArg)[]) char* { }
//! As normal toArgsPosix, but adds redirect after cmd with no processing.
fn toArgsPosix(cmd: scope (SinkArg), args: scope (SinkArg)[], redirect: scope (SinkArg)) char* { }
//! Parse a string as a series of arguments, like bash/make does.
fn parseArguments(str: scope (SinkArg)) string[] { }
fn escapeAndAddQuotation(sink: scope (Sink), str: scope (SinkArg))
Surround the string with " and escape " and /.
str
is surrounded by "
. Any "
present in str
will
be preceded by \
, as will any \
characters.
Examples
escapeAndAddQuotation(sink, `hello`); // "hello"
escapeAndAddQuotation(sink, `"hel\lo"`); // "\"hel\\lo\""
fn toArgsPosix(cmd: scope (SinkArg), args: scope (SinkArg)[]) char*
Returns a textual representation of cmd
and args
that can be passed to "/bin/sh -c".
Given an array of strings (say from parseArguments) create a C string with the elements separated by spaces and quotes as appropriate.
fn toArgsPosix(cmd: scope (SinkArg), args: scope (SinkArg)[], redirect: scope (SinkArg)) char*
As normal toArgsPosix, but adds redirect
after cmd
with
no processing.
fn parseArguments(str: scope (SinkArg)) string[]
Parse a string as a series of arguments, like bash/make does.
- The words in
str
are split by whitespace. - Whitespace isn't included unless it's in a string literal,
which uses the
"
character.
Example
parseArguments(`a b c "d e f"`); // Returns ["a", "b", "c", "d e f"]