I am using commander@2.9.0 . Attached the code below
package.json
{
"name": "commandtools",
"version": "1.0.0",
"description": "A command line example",
"main": "index.js",
"scripts": {
"test": "node index.js hello"
},
"author": "aaa <aaa@xxx.com>",
"license": "MIT",
"bin": {
"cmdtools":"./index.js"
},
"dependencies": {
"commander": "^2.9.0"
}
}
index.js
var program = require('commander');
program
.version('0.0.1')
.usage('<input>')
.parse(process.argv);
if(!program.args.length) {
program.help();
} else {
console.log('Input: ' + program.args);
}
On executing in command-line,
cmdtools Hello
index.js file opens without any output in the command-line
On executing,
npm test
The output is
Input: hello
What am I missing ?
Your code outputs the arguments passed in the command line.
npm test
outputs Input: hello
, because npm test
actually runs node index.js hello
. If you change node index.js hello
to node index.js banana
the output will be Input: banana
.
More about CLI arguments and how to access them here: https://nodejs.org/api/process.html#process_process_argv
cmdtools
command does not output anything, because there are no arguments passed to the index.js
file.
When running cmdtools
command you passed an argument to the cmdtools
command and not to index.js
. Nothing is outputted, because program.help()
does not output anything to the console. You can test this by running console.log('test')
instead of program.help()
.
The error in the npm module is
cmdtools Hello
and got the expected output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.