-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to run multiple commands together #886
Comments
So when you run node cli/pm.js --flag both When you run node cli/pm.js numbers 1 2 3 say hello
I am not sure if other parsing packages support multiple commands on single line, but it is unusual for a command line tool to do this. You might be able to still use node cli-test/pm.js numbers 1 2 3 --and say hello --font italic
or
node cli-test/pm.js numbers 1 2 3 + say hello --font italic
Then you could split up the args into the separate groups, and parse |
My use case for running multiple commands is spawning one or more long running processes (e.g. file watches) in addition to other short-lived execution commands. Each command was dynamically created per 'module' that supplied properties (name, options, etc) and the action callback. Thanks to @shadowspawn for the pointers of splitting the args up. /**
* Split CLI commands into distinct commands
* for commander to parse
*
* @async
* @param {array} args
* @return {Promise<array<string[]>>}
*/
async function splitCommands(args) {
const stringToSplit = args.splice(2).join(' ');
// Split commands on delimiter
return stringToSplit
.split(commandDelimiter)
.map((command) => {
const commandArray = command
.split(' ')
.filter((commandPart) => commandPart !== '');
return args.concat(commandArray);
});
}
/**
* Invoke commander parser per command
*
* @async
* @param {array<string[]>} commands
* @return {Promise<commander.Command[]>}
*/
async function parseCommands(commands) {
return await Promise.all(commands.map((command) => program.parseAsync(command)));
} Used for example like so: import commander from 'commander';
const program = new commander.Command();
const commandDelimiter = '+';
async function init() {
program
.storeOptionsAsProperties(false)
.passCommandToAction(false)
.version(version)
.name(name)
.description(description);
return Promise.resolve();
}
// Refactor in your implementation
const subCommand = new commander.Command('command1');
subCommand
.storeOptionsAsProperties(false)
.passCommandToAction(false)
.requireOption('-f, --foo')
.action((options) => console.log(`action1: ${options.foo}`);
program.addCommand(subCommand);
const subCommand = new commander.Command('command2');
subCommand
.storeOptionsAsProperties(false)
.passCommandToAction(false)
.requireOption('-b, --bar')
.action((options) => console.log(`action2: ${options.bar}`);
program.addCommand(subCommand);
init()
.then(() => doSomething())
.then((something) => doAnotherThing(something))
.then(() => splitCommands(process.argv))
.then((commands) => parseCommands(commands))
.catch((err) => console.log(err)); In shell:
|
I'm trying to build a CLI that need to run multiple commands together, some commands even have specific options. I also need to store the value passed on each commands in a variable for later use.
cli/pm.js
cli/pm-numbers.js
cli/pm-say.js
Test:
node cli/pm.js say hello
ornode cli/pm.js numbers 1 2 3
everything works finenode cli/pm.js --flag
print outerror: unknown option '--flag'
node cli/pm.js numbers 1 2 3 say hello
log outnumbers === [ 1, 2, 3, NaN, NaN ]
andsay === undefined
node cli-test/pm.js numbers 1 2 3 say hello --font italic
print outerror: unknown option '--font'
What am I doing wrong? Anyone can help?
The text was updated successfully, but these errors were encountered: