I wrote a cli program in nodejs, using commanderjs tool but when I tried, it can't get value from process.argv. please help this out.
//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")
}
I run
$ 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:
can't get value, why is that?
You need to use the opts function to access the parsed arguments. The version will be automnatically displayed with -V flag.