yargs / yargs-parser

:muscle: the mighty option parser used by yargs
http://yargs.js.org/
ISC License
493 stars 118 forks source link

Single dash passed as positional arg disappears #381

Open rrthomas opened 3 years ago

rrthomas commented 3 years ago

This is with yargs v16.0.1.

Sample code in test.js:

const yargs = require('yargs/yargs')
const {hideBin} = require('yargs/helpers');
const argv = yargs(hideBin(process.argv))
      .command('$0 <foo>', 'the default command', (yargs) => {
        yargs
          .positional('foo', {type: 'string', required: true})
      }, (argv) => {
        console.log(argv)
      })
      .argv

Test:

$ node test.js -
{ _: [], '$0': 'test.js', foo: '' }
$ node test.js a
{ _: [], '$0': 'test.js', foo: 'a' }

This is unfortunate, as using - as a filename to mean stdin or stdout is a common convention.

markus456 commented 3 years ago

So far the only workaround I've found is to use '"-"' as the argument. This preserves it as a positional argument.

shadowspawn commented 1 year ago

This may be an issue in yargs rather than yargs-parser. There is a test that dash is supported as a positional in yargs-parser: https://github.com/yargs/yargs-parser/blob/3aba24ceaa1a06ceb982c63a06002526d781e826/test/yargs-parser.cjs#L1496

Related: https://github.com/yargs/yargs/issues/1312