lukeed / lukeed/mri

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

Open
#25 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
681
Forks
22
PR merge metrics
No merged PRs in 30d

Description

When I have a config like `{ boolean: ["foo"], string: ["foo"] }`, I expect that passing `--foo` (without any values) to return `true` in the parsed object. Below shows the exact scenario:

```js
import mri from "mri";

const opts = { boolean: ["foo"], string: ["foo"] };

// ❓ cmd --foo
console.log(mri(["--foo"], opts));
// => { _: [], foo: '' }
// Expected: { _: [], foo: true }

// ❓ cmd --foo --bar
console.log(mri(["--foo", "--bar"], opts));
// => { _: [], foo: '', bar: true }
// Expected: { _: [], foo: true, bar: true }

// ✅ cmd --foo ""
console.log(mri(["--foo", ""], opts));
// => { _: [], foo: '' }

// ✅ cmd --foo "" --bar
console.log(mri(["--foo", "", "--bar"], opts));
// => { _: [], foo: '', bar: true }

// ✅ cmd --foo "str"
console.log(mri(["--foo", "str"], opts));
// => { _: [], foo: 'str' }

// ✅ cmd --foo "true"
console.log(mri(["--foo", "true"], opts));
// => { _: [], foo: 'true' }

// ✅ cmd --foo "false"
console.log(mri(["--foo", "false"], opts));
// => { _: [], foo: 'false' }

// ✅ cmd --no-foo
console.log(mri(["--no-foo"], opts));
// => { _: [], foo: false }
```

It's not documented that both boolean and string would work, but it seems to almost work as expected to me with the first case that should be `true`.

As a workaround, I'm doing something like this:

```js
const args = process.argv.slice(2);
const parsed = mri(args, ...)

if (parsed.foo === "" && args[args.indexOf("--foo") + 1] !== "") {
parsed.foo = true;
}
```

Related: https://github.com/changesets/changesets/pull/1392

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the mri entry point and reproduce the documented calls with boolean and string options, especially --foo and --foo --bar. Update the argument handling so an option without a value returns true in these cases, while preserving the shown empty-string and explicit-string results; verify all examples in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.