Skip to content

fix: parse boolean and string flag without value as true (resolves #25) - #33

Open
wwww-deeeee wants to merge 1 commit into
lukeed:masterfrom
wwww-deeeee:fix-issue-25-boolean-string-flag
Open

wwww-deeeee wants to merge 1 commit into
lukeed:masterfrom
wwww-deeeee:fix-issue-25-boolean-string-flag

Conversation

@wwww-deeeee

Copy link
Copy Markdown

Summary

Fixes #25: Boolean and string argument with no value is not parsed as true.

Root Cause

When a flag is configured in both opts.boolean and opts.string (e.g. { boolean: ['foo'], string: ['foo'] }), passing --foo without a value sets val = true. In toVal, the opts.string check was evaluated first, which treated val === true as empty string '' via val == null || val === true ? '' : String(val).

Changes

  1. src/index.js:
    • In toVal, prioritize checking if val === true when a key is present in both opts.boolean and opts.string, preserving true as the parsed value.
  2. test/index.js:
    • Added unit test suite for flags configured as both boolean and string (--foo -> true, --foo --bar -> true, --foo hello -> 'hello', --foo "" -> '').
    • All 88 tests pass (84 existing + 4 new).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Boolean and string argument with no value is not parsed as true

1 participant