minimistjs / minimist

parse argument options
MIT License
515 stars 30 forks source link

Document ability to disable numeric conversion for positional arguments (non-options) #52

Closed shadowspawn closed 5 months ago

shadowspawn commented 8 months ago

It is possible to disable the automatic conversion to numbers for positional arguments, and not just for options:

const argv = minimist(process.argv.slice(2), {
   string: ['_'] // disable numeric conversion of positional arguments (non-options)

I think worth documenting as it does fill out the control over conversions. (Although it does feel a bit of a trick.)

Background: I didn't realise this was possible until shown in: https://github.com/minimistjs/minimist/issues/51#issuecomment-1776814777

shadowspawn commented 2 months ago

For historical interest, was a related issue on the original Minimist repo asking for this functionality, with multiple comments and some upvotes:

Add option to disable numeric conversion for arguments https://web.archive.org/web/20200904203043/https://github.com/substack/minimist/issues/36


var argv = require('minimist')(process.argv.slice(2));

console.dir(argv);

If I invoke my command using my-command 2.0, I end up with something like this:

{ _: [ 2 ]}

I realize if this was an option I could use opts.string to tell minimist to treat my option as a string but how can I do this for arguments? This is very similar to issue minimist/issues/21 but the difference is that minimist/issues/21 could be fixed using a minimist option while I see no option for minimist arguments.

shadowspawn commented 2 months ago

And another historical issue suggesting should be documented:

Document that rest arguments have their type inferred too https://web.archive.org/web/20200904203142/https://github.com/substack/minimist/issues/32


I was not expecting rest arguments to have their type inferred. Might be a good idea to make that clear.

Also show how to force rest arguments to a specific type:

require('minimist')(process.argv.slice(2), {string: ['_']})