class Argvs {
constructor() {
this.argvsAll = this.argvsAll();
}
argvsAll() {
return process.argv.slice(2).reduce((acc, item) => {
item = item.split(/=/);
const [k, v] = [item[0].replace(/-/gi, ''), item[1]];
acc.push({
[k]: v
});
return acc;
}, [])
}
argvsGet(k) {
return this.argvsAll.reduce((acc, item) =>
acc ?
acc :
(k in item) ?
acc = item[k] :
acc, false)
}
}
// λ node server.js --name=ajanuw --post=14
let a = new Argvs();
console.log( a.argvsAll); // [ { name: 'ajanuw' }, { post: '14' } ]
console.log( a.argvsGet('name')); // ajanuw
console.log( a.argvsGet('post')); // 14
Node.js simply get command parameters
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324978835&siteId=291194637
Recommended
Ranking