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
when in singleton mode, calling parse() multiple times results in undefined behavior #1137
Comments
Any solution for this question? |
@rmlevangelio I don't know if anyone is working on this or not but I never managed to make things working for this case. In the end I just went with options arguments instead |
Seeing the same issue in command modules. Any solutions a year later? |
@varl odd, out of curiosity does it work with the chaining API, when you're not using command modules? I tend to use the chaining API and have never bumped into this. |
@mleguen @bcoe Thanks for looking into this. At the end of the day it looks like we are just plain old "doing it wrong" internally. 😅 We inadvertently wound up calling The first time the required argument is parsed, it is correctly picked up, but the second time ./cli-broken.js test foobar
should print the name: foobar
cli-broken.js test <name>
Delete tracked branches gone stale for remotes
Positionals:
name a name [string] [required] [choices: "foobar"]
Options:
--version Show version number [boolean]
-h, --help Show help [boolean]
Missing required argument: name I've condensed the implementation which spans multiple repositories and tools into a single one here: https://github.com/varl/yargs-1137-reproduction Is there something I can do to help the devs who implement command modules for our CLI tool avoid inadvertently calling |
@varl I suggest you use yargs's But as commented in #1144 do not check the |
@mleguen Thanks for taking the time to check it out and for pointing out the gotcha! I will change it in accordance to your suggestion. 👍 |
@mleguen thanks for helping dig into this 👍 @varl if I understand correctly, was the behavior different if you called:
i.e., calling parse twice gave different results ... this I don't love, and should be fixed (even if calling it once is a good workaround). |
@bcoe Yes, the behavior was different. A minimal example#!/usr/bin/env node
const yargs = require('yargs')
let argv = yargs
.command(
'test <name>',
'prints foobar if foobar is passed',
yargs => yargs.positional('name', {
aliases: 'n',
describe: 'a name',
choices: ['foobar'],
type: 'string',
}),
argv => console.log(`should print the name: ${argv.name}`)
)
argv = yargs.parse()
argv = yargs.parse() The result from a run./cli-double.js test foobar
should print the name: foobar
cli-double.js test <name>
Delete tracked branches gone stale for remotes
Positionals:
name a name [string] [required] [choices: "foobar"]
Options:
--version Show version number [boolean]
-h, --help Show help [boolean]
Missing required argument: name First it prints the correct line: "should print the name: foobar" and when It behaves as if the args have already been consumed and aren't available the second time |
Issue reproduced with @varl 's example. |
The problem comes from So in the second call to We have to find a way to "unreset" |
I suggest to extend the
The alternative is to copy yargs instead of resetting it to handle nested commands parsing, but would require much more work. @bcoe what do you think of this? |
Forget it. It breaks dozens of tests. In fact, I understand we are indeed expecting Lines 824 to 831 in 266ed91
So I frankly do not see how to allow calling Does someone have another idea? |
I finally found a way not to break anything by freezing/unfreezing directly in parse, as it was already done when calling parse with arguments. I should be able to submit a PR soon. |
@btruhand if you convince you to try this out on your original bug, and see if it does the trick for you? |
@bcoe thanks for everyone's dedicated support. Am not using it anymore but I would try it out tomorrow out of curiosity |
So my situation is that I want to use a required positional argument with some non-required options. To my surprise the positional argument is not picked up as I expected. Here is what the command look like
If I try to give an argument that is not part of the argument choice this is what I get:
So obviously the argument is properly picked up. Not sure what is going on here. This is what the command option looks like:
The text was updated successfully, but these errors were encountered: