@gkalpak/cli-utils
Version:
A private collection of utilities for developing cli tools.
54 lines • 2.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const command_utils_1 = require("../lib/command-utils");
const internal_utils_1 = require("../lib/internal-utils");
/**
* Expand a command string by substituting argument identifiers with the specified arguments. It also supports
* default/fallback arguments (specified either as static values or as commands to execute and use the output).
*
* The first argument is the command to be expanded. The rest of the arguments are passed to
* {@link CommandUtils#preprocessArgs preprocessArgs()} (to separate actual arguments from configuration arguments) and
* the result is expanded using {@link CommandUtils#expandCmd expandCmd()}.
*
* @example
* ```
* gkcu-expand-cmd "echo \$1 \${2:bar} \$1" foo
* #--> echo foo bar foo
*
* gkcu-expand-cmd "echo \$1 \${2:bar} \$1" foo BAZ
* #--> echo foo BAZ foo
*
*
* gkcu-expand-cmd "git checkout \${1:master} \$2*"
* #--> git checkout master
*
* gkcu-expand-cmd "git checkout \${1:master} \$2*" foo
* #--> git checkout foo
*
* gkcu-expand-cmd "git checkout \${1:master} \$2*" foo -b qux
* #--> git checkout foo -b qux
*
*
* gkcu-expand-cmd "echo \${1:Hello}, \${0:::whoami}!"
* #--> echo Hello, gkalpak!
*
* gkcu-expand-cmd "echo \${1:Hello}, \${0:::whoami}!" Hey
* #--> echo Hey, gkalpak!
* ```
*
* @param cmd - The command to expand.
* @param ...rawArgs - The arguments, including both runtime arguments (that will be used for substituting) and
* {@link commandUtils#IRunConfig configuration arguments}.
*
* @return The expanded command, with arguments substituted (including running default/fallback value sub-commands, as
* necessary).
*/
if (require.main === module) {
const [cmd, ...rawArgs] = process.argv.slice(2);
const { args, config } = command_utils_1.commandUtils.preprocessArgs(rawArgs);
command_utils_1.commandUtils.
expandCmd(cmd, args, config).
then(console.log, internal_utils_1.internalUtils.onError.bind(internal_utils_1.internalUtils));
}
//# sourceMappingURL=expand-cmd.js.map