Skip to content

Positional arguments eat quotes #1324

Open

Description

@OJezu

Expected: quoted qutoes (I.e. '"hello"') should be left alone, only the OS should remove quotes when constructing process' argv.

What happens: yargs also removes quotes.

const yargs = require("yargs");

yargs.command('$0 <input> <input2>', "some input");

console.log(process.argv);
console.log(yargs.argv);

Output:

$ node ./test-yargs.js '"hello"' '""world""'
[ '/usr/bin/node',
  './test-yargs.js',
  '"hello"',
  '""world""' ]
{ _: [],
  '$0': 'test-yargs.js',
  input: 'hello',
  input2: '"world"' }
$ node ./test-yargs.js "'yargs'" "are hungry"
[ '/usr/bin/node',
  '/home/krzysztofchrapka/dev/src/node-amqp-tools/test-yargs.js',
  '\'yargs\'',
  'are hungry' ]
{ _: [],
  '$0': 'test-yargs.js',
  input: 'yargs',
  input2: 'are hungry' }

Activity

  1. changed the title [-]Positional arguments eats quotes[/-] [+]Positional arguments eat quotes[/+] on Mar 22, 2019
  2. jedwards1211 commented on Mar 9, 2021

    @jedwards1211

    Not just positional arguments, options eat quotes too. Is some kind of JSON parsing going on? Should have been documented from the get-go

  3. jly36963 commented on Jul 20, 2021

    @jly36963
    Contributor

    I'm using yargs@17, and this is what I observed:

    yargs
      .command({
        command: "cmd <pos1> <pos2>",
        desc: "cmd desc",
        builder: (yargs) =>
          yargs
            .positional("pos1", {
              desc: "pos1 desc",
              type: "string",
            })
            .positional("pos2", {
              desc: "pos2 desc",
              type: "string",
            })
            .option("opt1", {
              desc: "opt1",
              type: "string",
            }),
        handler: (argv) => {
          console.log(argv);
        },
      })
      .help()
      .parse('cmd ""Hello"" ""World"" --opt1 "":)""');

    logs:

    {
      _: [ 'cmd' ],
      opt1: '":)"',
      '$0': 'test-issue.js',
      pos1: '"Hello"',
      pos2: '"World"'
    }
    

    I wrapped each of the positionals/options with extra quotes, and they were preserved.

    I don't think this problem exists anymore. At some point they must have resolved the issue.

  4. danopia commented on Jul 20, 2021

    @danopia

    The issue is still there, it's with the argv / pre-split mode. If you put your code into a file and pass the args in (.argv instead of .parse()) you'll see it even with [email protected].

    $ node repro.js cmd '"Hello"' '"World"' --opt1 '":)"'
    {
      _: [ 'cmd' ],
      opt1: ':)',
      '$0': 'repro.js',
      pos1: 'Hello',
      pos2: 'World'
    }
    

    Alternatively, this parse call reproduces the same incorrect result:
    .parse(['cmd', '"Hello"', '"World"', '--opt1', '":)"']);


    Additionally, note that ""..."" is not a double quoting in shell, in fact the body will be unquoted and each pair of two quotes represent empty strings. So that's three sequential string literals that get concatenated. This is why I had to do '"..."' in my above repro.

    If you run .parse('cmd ""Hello World"" --opt1 "":) haha""') you'll see how yargs doesn't actually obey that quoting style:

    {
      _: [ 'cmd', 'haha""' ],
      opt1: '"":)',
      '$0': 'repro.js',
      pos1: '""Hello',
      pos2: 'World""'
    }
    

    This splitting is faithful to the shell interpretation of those quotes, however to fully reflect a shell, the empty double quotes should've been dropped. I suspect that the quote dropping is being done at the wrong part of the parse pipeline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions