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

yargs.option("version") is admitted, but fails silently #2199

Open
JJ opened this issue Jun 20, 2022 · 6 comments
Open

yargs.option("version") is admitted, but fails silently #2199

JJ opened this issue Jun 20, 2022 · 6 comments
Labels

Comments

@JJ
Copy link

JJ commented Jun 20, 2022

Using something like:

yargs.option("version", {
                type: "string",
                default: "0.0.1",
                describe: "→ Version string for the package.json",
            });

Will not raise any kind of error or warning, but it will make parsing fail and print the version string instead.

@bcoe
Copy link
Member

bcoe commented Jun 20, 2022

@jly36963 didn't you do some work in this area already, regarding warning when version was used in unexpected ways?

@RoBorg
Copy link

RoBorg commented Mar 23, 2023

I just came across basically the same issue, but using a positional:

import yargs from 'yargs';

yargs(process.argv.slice(2))
    .command(
        'install [version]',
        'Install a thing',
        (yargs) => { yargs.positional('version', { default: 'hello' }) },
        (argv) => console.log(`Installing ${argv.version}`)
    ).parse();

The problem seems to be when you add a default - if this is omitted then the warning is correctly shown.

@shadowspawn
Copy link
Member

A warning about a name collision with the default version option got added in #1986. Unfortunately, it turns out process.emitWarning() is not synchronous so the process.exit() called after showing the version terminates the program before the warning is displayed!

By default, whenever a warning is issued using process.emitWarning(), Node.js will print that warning to stderr on process.nextTick().

https://medium.com/@jasnell/introducing-process-warnings-in-node-v6-3096700537ee

@shadowspawn
Copy link
Member

shadowspawn commented Mar 24, 2023

The warning (which should have been displayed) explains the issue and possible work-arounds:

(node:31602) Warning: "version" is a reserved word.
Please do one of the following:
- Disable version with `yargs.version(false)` if using "version" as an option
- Use the built-in `yargs.version` method instead (if applicable)
- Use a different option key

@shadowspawn
Copy link
Member

shadowspawn commented Mar 24, 2023

(I will re-label this as a bug since the warning is not appearing in some circumstances.)

@shadowspawn
Copy link
Member

@RoBorg correctly identified the additional factor which means the warning does not get displayed.

When the author supplied version option or positional has a default value, this triggers the built-in version handler which assumes the user has specified --version and so displays the version and exits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants