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

parse/parseAsync cannot be used multiple times on the same Command instance #2263

Closed
goodov opened this issue Oct 9, 2024 · 6 comments
Closed

Comments

@goodov
Copy link

goodov commented Oct 9, 2024

Not sure if it's a known gotcha or an actual bug, but I've noticed that I can't use parseAsync multiple times with different args on the same Command instance (for ex. in tests), because internally all parsed options are cached.

@goodov
Copy link
Author

goodov commented Oct 9, 2024

Hm. Found multiple closed duplicates which are all solved by referring to the added text in README.md. How about having this info in the parse/parseAsync method comment? This is pretty important bit that I wasn't able to find by looking into the source closely.

  /**
   * Parse `argv`, setting options and invoking commands when defined.
   *
   * Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
   *
   * The default expectation is that the arguments are from node and have the application as argv[0]
   * and the script being run in argv[1], with user parameters after that.
   *
   * @example
   * ```
   * program.parseAsync(process.argv);
   * program.parseAsync(); // implicitly use process.argv and auto-detect node vs electron conventions
   * program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
   * ```
   *
   * @returns Promise
   */
  parseAsync(argv?: readonly string[], options?: ParseOptions): Promise<this>;

@goodov
Copy link
Author

goodov commented Oct 9, 2024

Throwing might be the best thing to do as another user mentioned here: #2246 (comment)

This should be fixed.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Oct 11, 2024

There were no related issues opened for a while after the note got added to the README in #2036, but this issue and two others just recently:

I expect prohibiting repeated calls will break some programs, but I also expect most of the cases will be technically unsafe and the author had not noticed there is state left behind from the first parse.

I am wondering whether to throw an error for multiple calls to parse in the next major version.

@goodov
Copy link
Author

goodov commented Oct 15, 2024

I expect prohibiting repeated calls will break some programs, but I also expect most of the cases will be technically unsafe and the author had not noticed there is state left behind from the first parse.

yep, I think this is what everyone are doing right now until they figure that something is wrong.

Throwing is a fast and simple take on the problem. Supporting multiple calls to parse would be a much better solution for the library in terms of the usability. It looks like it supposed to work like that, but was never carefully supported in this area which led to some invariants being broken with time. I don't see why we can't support this, it's just a matter of resetting the state on each call.

@shadowspawn
Copy link
Collaborator

Opened a PR to allow multiple calls to .parse(): #2299

@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Dec 27, 2024
@shadowspawn
Copy link
Collaborator

Support for multiple parse added in version 13.0.0

@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Dec 30, 2024
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

2 participants