node.js - How can I access command options in separate files? -
i'm using commander specify commands , options global node module in index.js file of project (like shown in example).
i know can check if command used using following code:
if (program.peppers) { console.log('-peppers used') } but how can check properties in other files? i've tried exporting program , requiring in other files, doesn't seem work.
let's want check if option used in different file in 1 in i've defined them. how should that?
you pass parsed program files/functions need parsed arguments or can export parsed arguments program instance
file index.js
var program = require('./program'); if (program.peppers) { console.log('-peppers used') } file program.js
var program = require('commander'); program .version('0.0.1') .option('-p, --peppers', 'add peppers') .option('-p, --pineapple', 'add pineapple') .option('-b, --bbq-sauce', 'add bbq sauce') .option('-c, --cheese [type]', 'add specified type of cheese [marble]', 'marble') .parse(process.argv); module.exports = program; invoking index.js command line:
> node index.js -p yields output
-peppers used
Comments
Post a Comment