我在nodejs中编写了一个cli程序,使用commandjs工具但是当我尝试时,它无法从process.argv中获取价值。请帮助解决这个问题。
//cli.js
const program = require('commander');
program
.version("0.0.1")
.option("-a, --add", "add something")
.option("-u, --update", "update something")
.option("-r, --remove", "remove something")
.parse(process.argv);
console.log(`version ${program.version} You Choose: `);
if (program.add) {
console.log(" add something")
}
if (program.update) {
console.log(" update something")
}
if (program.remove) {
console.log(" remove something")
}
我跑
$ node cli.js --add
version version(str, flags, description) {
if (str === undefined) return this._version;
this._version = str;
flags = flags || '-V, --version';
description = description || 'output the version number';
const versionOption = this.createOption(flags, description);
this._versionOptionName = versionOption.attributeName();
this.options.push(versionOption);
this.on('option:' + versionOption.name(), () => {
this._outputConfiguration.writeOut(`${str}\n`);
this._exit(0, 'commander.version', str);
});
return this;
} You Choose:
无法获得价值,这是为什么呢?