Skip to content

parseArgs with type: "boolean" and default: true does not allow a false value #53095

Closed
@mcollina

Description

@mcollina

Consider the following example:

import { parseArgs } from 'node:util'

const options = {
  foo: {
    type: 'boolean',
    short: 'f',
    default: true
  },
};

function parseAndLog (args, options) {
  console.log('---');
  try {
    console.log('args:', args);
    const {
      values,
      positionals,
    } = parseArgs({ args, options });
    console.log(values, positionals);
  } catch (e) {
    console.error(e);
  }
}

parseAndLog(['--foo', 'false'], options);
parseAndLog(['--no-foo'], options);

How can somebody add a flag that can be defaulted as true, but allow a --no flag to set it to false? I imagine that a type: "boolean" setting would add both --flag and --no-flag.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssues that are suitable for first-time contributors.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions