minimistjs / minimist

parse argument options
MIT License
530 stars 30 forks source link

`-` can not be string value #15

Closed Yesterday17 closed 1 year ago

Yesterday17 commented 1 year ago

The regex /^-/ matches -, making argument valued by - character only not available. - is commonly used as the reference of stdin.

https://github.com/minimistjs/minimist/blob/5784b17f4905939c14037b2e80e36c62b7d0e68b/index.js#L167

ljharb commented 1 year ago

This line dates back to https://github.com/minimistjs/minimist/commit/7cced88d82e399d1a03ed23eb667f04d3f320d10#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346R63 - iow, it's always been in minimist (and presumably optimist, its precursor, as well).

I agree that an argument value of - should be permitted. Want to send a PR with a test case, and perhaps also a fix?

Yesterday17 commented 1 year ago

Maybe /^-./ could work?

shadowspawn commented 1 year ago

The test is done in two places, for a short option and for a long option, and I think should be the same. The pattern used in the other place is more careful and won't fire for a plain -: /^(-|--)[^-]/

https://github.com/minimistjs/minimist/blob/5784b17f4905939c14037b2e80e36c62b7d0e68b/index.js#L220

milanholemans commented 1 year ago

We are having a similar issue, we are trying to run a command with option --id "-9rMKQooUjZdxgv1qQVZYABEuw". After parsing, it looks like:

{
  "1": true,
  "9": true,
  "i": true,
  "r": true,
  "M": true,
  "K": true,
  "Q": true,
  "o": true,
  "U": true,
  "j": true,
  "Z": true,
  "d": true,
  "x": true,
  "g": true,
  "v": true,
  "q": true,
  "V": true,
  "Y": true,
  "A": true,
  "B": true,
  "E": true,
  "u": true,
  "w": true
}

Id should be parsed as string and not as flags.

shadowspawn commented 1 year ago

@milanholemans Minimist assumes arguments which start with a dash are an option (or options) and not consumed as an option argument. A fix for single dash won't change this behaviour.

To get the parsing you expect, use an embedded option value with an = like:

--id=-9rMKQooUjZdxgv1qQVZYABEuw

Then a possible dash on the start of the id will not change the parsing.

milanholemans commented 1 year ago

Minimist assumes arguments which start with a dash are an option (or options) and not consumed as an option argument

I assumed this was the problem indeed.

To get the parsing you expect, use an embedded option value with an = like:

This seems to be working indeed, awesome! Thanks for the fast feedback @shadowspawn!

topaz1008 commented 1 year ago

Hello.

I also have the same problem, and the suggestions here don't solve it 100%. I am using Windows10 and Node.js v18.12.1

To get the parsing you expect, use an embedded option value with an = like:

This does not work if the value contains a space. you need both an =, and quotation marks " around the parameter. e.g.

> node test.js --command="-c:v copy"
{ _: [ 'copy' ], command: '-c:v' }

Describe the exact steps which reproduce the problem

// test.js
import minimist from 'minimist';

const options = minimist(process.argv.slice(2));
console.log(options);

Running the following command gives this output:

> node test.js --command "-c:v copy"
{ _: [], command: true, c: ':v copy' }

Expected output:

> node test.js --command "-c:v copy"
{ _: [], command: '-c:v copy' }

When writing out the command using BOTH an = (equals) and quotation marks, it works as expected. e.g.

> node test.js --command="-c:v copy"
{ _: [], command: '-c:v copy' }