Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rDr4g0n opened this issue Mar 15, 2023 · 1 comment · Fixed by #32
Closed

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

rDr4g0n opened this issue Mar 15, 2023 · 1 comment · Fixed by #32
Labels
bug Something isn't working

Comments

@rDr4g0n
Copy link

rDr4g0n commented Mar 15, 2023

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 shadowspawn added the bug Something isn't working label Mar 15, 2023
@shadowspawn
Copy link
Collaborator

Thanks for clear description and reproduction.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants