Could someone explain me why i have troubles retrieving the option '-x' (see below)?
Do I have to escape some characters ?
Beyond that:
- why this is working:
node b.js parse url1 url2 url3 -x
- but this is not:
npm run babel-node a.js parse url1 url2 url3 -x
here is my package.json file
{
"name": "commander_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"babel-node": "babel-node --presets=env",
"a": "npm run babel-node a.js parse url1 url2 url3 -x",
"b": "node b.js parse url1 url2 url3 -x ",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.18.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1"
}
}
result of npm run a :
> commander_test@1.0.0 a D:\repositories\node\commander > npm run babel-node a.js parse url1 url2 url3 -x > commander_test@1.0.0 babel-node D:\repositories\node\commander > babel-node --presets=env "a.js" "parse" "url1" "url2" "url3" [ 'node', 'D:\\repositories\\node\\commander\\a.js', 'parse', 'url1', 'url2', 'url3' ] parse : url1 => undefined parse : url2 => undefined parse : url3 => undefined
result of npm run b
> commander_test@1.0.0 b D:\repositories\node\commander > node b.js parse url1 url2 url3 -x [ 'C:\\Program Files\\nodejs\\node.exe', 'D:\\repositories\\node\\commander\\b.js', 'parse', 'url1', 'url2', 'url3', '-x' ] parse : url1 => true parse : url2 => true parse : url3 => true
the 2 files a.js and b.js are the same . just a.js uses es6 modules and b.js Commonjs modules
import program from "commander" // a.js
let program = require("commander") // b.js
//from here it's the exact same code
console.log(process.argv)
program
.version('0.0.1')
.command('parse <urls...> ')
.option('-x, --opt', 'opt')
.action( function (urls, cmd) {
urls.forEach(url => {
console.log(`parse : ${url} => ${cmd.opt}`)
});
})
program.parse(process.argv)