minimistjs / minimist

parse argument options
MIT License
515 stars 30 forks source link

Parsing -- when quoted does an unexpected thing #21

Closed Ankcorn closed 1 year ago

Ankcorn commented 1 year ago

When I run minimist with the following input

[
  '--add',
  '--env',
  'staging',
  'NODE_OPTIONS',
  '--require node_modules/test/index'
]

The final arg is added as a named argument.

{
  _: [ 'NODE_OPTIONS' ],
  add: true,
  a: true,
  debug: false,
  d: false,
  remove: false,
  r: false,
  verbose: false,
  v: false,
  env: 'staging',
  e: 'staging',
  'require node_modules/test/index': true
}

My desired output would be

{
  _: [ 'NODE_OPTIONS' , '--require node_modules/test/index'],
  add: true,
  a: true,
  debug: false,
  d: false,
  remove: false,
  r: false,
  verbose: false,
  v: false,
  env: 'staging',
  e: 'staging',
}

I suspect this is a bug

shadowspawn commented 1 year ago

The behaviour is as expected. Options may appear before or after positional arguments, and the argument is being treated as an option since it starts with a double-dash.

To get the behaviour you want, use -- to stop the option processing. Say:

[
  '--add',
  '--env',
  '--',
  'staging',
  'NODE_OPTIONS',
  '--require node_modules/test/index'
]
Ankcorn commented 1 year ago

Thank you :smiling_face_with_tear:

shadowspawn commented 1 year ago

I'll add one more thing.

-- is good for when the end-user is trying to pass something tricky.

If as author you always want this behaviour and have a usage pattern where there are some plain positional arguments before any with a dash, you might be able to use opts.stopEarly. It is intended more for implementing subcommands but might be useful in this case too.