minimistjs / minimist

parse argument options
MIT License
515 stars 30 forks source link

Boolean option consumes next positional parameter #54

Closed svenyonson closed 7 months ago

svenyonson commented 7 months ago

If I have two boolean args followed by a positional parameter, the second boolean arg is no longer a boolean, it has the value of the positional parameter.

    #!/usr/bin/env node

     const minimist = require('minimist');

    // Define available options
    const options = {
        pvc: {
            alias: 'p', 
            type: 'boolean', 
            default: false, 
        },
        service: {
            alias: 's', 
            type: 'boolean', 
            default: false, 
        },
        help: {
            alias: 'h', 
            type: 'boolean', 
            default: false, 
        }
      };

    const args = minimist(process.argv.slice(2), options);

    if (args.help || args._.length == 0) {
        console.log("usage: ");
    }

    console.log(args);
node test-args.js -ps foo flim flam
{ _: [ 'flim', 'flam' ], p: true, s: 'foo' }
shadowspawn commented 7 months ago

Thanks for the example code. The problem is your declaration for the expected options is not in the format that Minimist uses. The opts properties described in the README are each at the top level, rather than per individual option.

Here is a refactored example (with the defaults left out since booleans default to false automatically):

#!/usr/bin/env node

const minimist = require('minimist');

// Define available options
const options = {
    boolean: ['pvc', 'service', 'help'],
    alias: { pvc: 'p', service: 's', help: 'h' }
  };

const args = minimist(process.argv.slice(2), options);

if (args.help || args._.length == 0) {
    console.log("usage: ");
}

console.log(args);
% node fixed.js -ps foo flim flam
{
  _: [ 'foo', 'flim', 'flam' ],
  pvc: true,
  p: true,
  service: true,
  s: true,
  help: false,
  h: false
}
svenyonson commented 7 months ago

Ah, thanks John.

The perils of using AI for code snippets..

On Dec 13, 2023, at 12:15 PM, John Gee @.***> wrote:

Thanks for the example code. The problem is your declaration for the expected options is not in the format that Minimist uses. The opts properties described in the README are each at the top level, rather than per individual option.

Here is a refactored example (with the defaults left out since booleans default to false automatically):

!/usr/bin/env node

const minimist = require('minimist');

// Define available options const options = { boolean: ['pvc', 'service', 'help'], alias: { pvc: 'p', service: 's', help: 'h' } };

const args = minimist(process.argv.slice(2), options);

if (args.help || args._.length == 0) { console.log("usage: "); }

console.log(args); % node fixed.js -ps foo flim flam { _: [ 'foo', 'flim', 'flam' ], pvc: true, p: true, service: true, s: true, help: false, h: false } — Reply to this email directly, view it on GitHub https://github.com/minimistjs/minimist/issues/54#issuecomment-1854561519, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC2KLWLFZK5DOWYURFHDADYJH5DRAVCNFSM6AAAAABATSGWLCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNJUGU3DCNJRHE. You are receiving this because you authored the thread.