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

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

Closed
mcollina opened this issue May 22, 2024 · 6 comments
Closed
Labels
good first issue Issues that are suitable for first-time contributors. util Issues and PRs related to the built-in util module.

Comments

@mcollina
Copy link
Member

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.

@kylo5aby
Copy link
Contributor

I see many node configuration use this bidirectional flag format, I'm willing to implement it in parseArgs

@mcollina
Copy link
Member Author

The bidirectional flag format is really common here because it allows to change the default of the flag in a follow-up major.

@MoLow MoLow added util Issues and PRs related to the built-in util module. good first issue Issues that are suitable for first-time contributors. labels May 22, 2024
@bakkot
Copy link

bakkot commented May 22, 2024

This can be done using the tokens array, though it does require about a dozen lines of boilerplate.

@shadowspawn
Copy link
Member

shadowspawn commented May 24, 2024

In fact, the same example of adding support for negation from the parseArgs repo (linked above) is also used as the example use of tokens in the Node.js documentation:

nodejs-github-bot pushed a commit that referenced this issue Jun 19, 2024
PR-URL: #53107
Refs: #53095
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
targos pushed a commit that referenced this issue Jun 20, 2024
PR-URL: #53107
Refs: #53095
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
EliphazBouye pushed a commit to EliphazBouye/node that referenced this issue Jun 20, 2024
PR-URL: nodejs#53107
Refs: nodejs#53095
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
bmeck pushed a commit to bmeck/node that referenced this issue Jun 22, 2024
PR-URL: nodejs#53107
Refs: nodejs#53095
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
@bakkot
Copy link

bakkot commented Jul 16, 2024

Fixed by #53107, presumably?

@RedYetiDev
Copy link
Member

Fixed by #53107, presumably?

I believe that was the intent, so I've closed this

marco-ippolito pushed a commit that referenced this issue Jul 19, 2024
PR-URL: #53107
Refs: #53095
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
marco-ippolito pushed a commit that referenced this issue Jul 19, 2024
PR-URL: #53107
Refs: #53095
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issues that are suitable for first-time contributors. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

6 participants