2

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 :

> [email protected] a D:\repositories\node\commander > npm run babel-node a.js parse url1 url2 url3 -x > [email protected] 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

> [email protected] 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) 
1
  • Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. Feel free to drop me a comment in case you have further questions or feedback for me. Commented Sep 11, 2018 at 8:19

1 Answer 1

2

i saw the anwser [https://github.com/babel/babel/issues/1730#issuecomment-111247861]

Thanks to Gajus here is the solution : he wrotes:

To avoid any confusion for those who stumble across this thread in the future, here is an example of how to use --:

babel-node --a -- ./foo --b In this case:

--a is an option that is passed to node (e.g. --debug or --throw-deprecation). --b is an option that is passed to the script, i.e., available under process.argv.

according to this solution i changed my scripts to :

 "scripts": { "a": "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" } 

and it worked !!

thanks

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.