Skip to content

script-utils/cli-utils

Contains utility classes and functions for managing task results, including success, exit codes, and chaining multiple tasks.

ClassDescription
CliTaskResultAbstract class representing the result of a task. Includes methods for handling success, exit codes, and chaining tasks.
FunctionDescription
cmdEscapeCommandLineEscapes cmd.exe metacharacters with ^ so that cmd.exe passes them through literally. This is necessary because cmd.exe’s " handling differs from CommandLineToArgvW and cannot be relied upon. Apply this to a command line string that will be executed via cmd.exe (e.g., spawn(cmd, [], \{ shell: true \}) on Windows).
toCommandLineConverts an array of command-line arguments into a single command-line string using the CommandLineToArgvW convention (the standard used by the Microsoft C runtime and most Windows programs). Implements the ArgvQuote algorithm from Everyone quotes command line arguments the wrong way: backslashes before quotes and at the end of a quoted argument are doubled. This produces a shell-agnostic command line. Callers that route through a specific shell (cmd.exe, PowerShell, sh) must apply shell-specific escaping on top — see cmdEscapeCommandLine.
toPosixCommandLineConverts an array of command-line arguments into a single command-line string quoted for a POSIX shell (sh, bash, zsh, …). Each argument is wrapped in single quotes unless it consists solely of characters that are safe unquoted, mirroring Python’s shlex.quote. A literal single quote inside an argument is emitted as the '\'' idiom (close the quoted span, add an escaped literal quote, reopen), so the shell reproduces the argument verbatim — no variable expansion ($), command substitution (`), globbing (*, ?, [), or word splitting. Use this on platforms where the command is executed through sh -c. On Windows, use toCommandLine plus cmdEscapeCommandLine instead — the two conventions are not interchangeable.
wrapCliTaskWraps a CLI task function to ensure it runs safely and handles its CliTaskResult.