minimistjs / minimist

parse argument options
MIT License
515 stars 30 forks source link

A boolean's default value does not work when an alias is used #29

Closed rDr4g0n closed 1 year ago

rDr4g0n commented 1 year ago

When a boolean with an alias uses the alias to define the default value, the default is not applied. This would not be bug-worthy if all defaults required using the actual flag name instead of an aliased name, but defaulting string flags works fine with aliased names. This produces some inconsistency in the behavior of the app as string vs boolean differ.

It seems that boolean defaults are only looked up by their actual flag name and does not include lookups by their aliased name: https://github.com/minimistjs/minimist/blob/main/index.js#L132-L134

Here is a repro for the bug:

#!/usr/bin/env node

const argv = require("minimist")(process.argv.slice(2), {
  boolean: ["a-boolean"],
  alias: {
    "a-boolean": "aBoolean",
  },
  default: {
    aBoolean: true,
  },
});

And the results:

$ ./index.js
received args: [ ]
parsed args: { _: [ ], 'a-boolean': false, 'aBoolean': false }

Here is a workaround (not using the aliased name) that avoids the bug:

#!/usr/bin/env node

const argv = require("minimist")(process.argv.slice(2), {
  boolean: ["a-boolean"],
  alias: {
    "a-boolean": "aBoolean",
  },
  default: {
    'a-boolean': true,
  },
});

And the results:

$ ./index.js
received args: [ ]
parsed args: { _: [ ], 'a-boolean': true, 'aBoolean': true }
shadowspawn commented 1 year ago

Thanks for clear description and reproduction.

In most cases the key and aliases can be used interchangeably. I agree it feels like a bug.