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

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

Open
bluwy opened this issue Jun 24, 2024 · 0 comments
Open

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

bluwy opened this issue Jun 24, 2024 · 0 comments

Comments

@bluwy
Copy link

bluwy commented Jun 24, 2024

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:

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:

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

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

Related: changesets/changesets#1392

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

No branches or pull requests

1 participant