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 :

> 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)
GhostCat
  • 137,827
  • 25
  • 176
  • 248
kas
  • 125
  • 1
  • 9
  • 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. – GhostCat Sep 11 '18 at 08:19

1 Answers1

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

kas
  • 125
  • 1
  • 9