yargs / yargs-parser

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

Invalid parsing of quoted arguments #344

Open medikoo opened 3 years ago

medikoo commented 3 years ago

Let's say I invoke a CLI with following arguments:

$ cli --docker-args "-p 5432:5432"

In Node.js trailing part of process.argv resolves to:

["--docker-args", "-p  5432:5432"]

Now when I put it to yargs-parser, result is not expected:

const yargsParser = require('yargs-parser');
console.log(yargsParser(["--docker-args", "-p  5432:5432"]));
// {
//   'docker-arg': true,
//   dockerArg: true,
//   p: ' 5432:5432'
// }

While expected result is:

{ "dockerArg": "-p  5432:5432" }
shadowspawn commented 1 year ago

An option argument that starts with a dash is problematic as Yargs assumes it might be an option instead. You can set nargs to make the option "greedy" and consume option arguments that start with a dash.

A user work-around (no author action required) is to use an equals: --docker-args="-p 5432:5432"

shadowspawn commented 1 year ago

I didn't make this clear in my previous comment, but the quotes get stripped by the shell interpreter before node or yargs see them. You can confirm this for yourself by writing out process.argv. [Edit: as you noted in your comment!]

What yargs is working with is ['--docker-args' , '-p 5432:5432']