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

Boolean option does not reset between parse() and parseAsync() calls #2247

Closed
edmsamuel opened this issue Sep 3, 2024 · 4 comments
Closed

Comments

@edmsamuel
Copy link

Español:

Descripción del Problema:
Al usar commander.js, la opción booleana --json no se restablece correctamente cuando se analiza varias veces. En el siguiente ejemplo, la opción --json se mantiene en true después de que se establece, incluso cuando no se pasa en la siguiente llamada a parse. Esto causa un comportamiento inesperado, ya que la opción debería restablecerse a false si no se incluye en la entrada.

Código de ejemplo:

const { Command, createCommand } = require('commander');
const program = createCommand();

const tmp = program
  .option('--json', 'output extra debugging', false)
  .action(options => console.log(options));

const r1 = program.parse(["node", "app.js"]);
const r2 = program.parse(["node", "app.js", "--json"]);
const r3 = program.parse(["node", "app.js"]);

Salida esperada:

{ json: false }
{ json: true }
{ json: false }

Salida actual:

{ json: false }
{ json: true }
{ json: true }

Comportamiento esperado:
La opción --json debería ser false en la última llamada a parse, pero se mantiene en true, lo que indica que no se restablece correctamente entre llamadas.

Inglés:

Problem Description:
When using commander.js, the boolean option --json does not reset correctly when parsing multiple times. In the following example, the --json option remains true after it is set, even when it is not passed in the subsequent parse call. This leads to unexpected behavior, as the option should reset to false if not included in the input.

Example Code:

const { Command, createCommand } = require('commander');
const program = createCommand();

const tmp = program
  .option('--json', 'output extra debugging', false)
  .action(options => console.log(options));

const r1 = program.parse(["node", "app.js"]);
const r2 = program.parse(["node", "app.js", "--json"]);
const r3 = program.parse(["node", "app.js"]);

Expected Output:

{ json: false }
{ json: true }
{ json: false }

Actual Output:

{ json: false }
{ json: true }
{ json: true }

Expected Behavior:
The --json option should be false in the final parse call, but it remains true, indicating that it is not resetting properly between calls.


Evidencia / Evidence

image

@shadowspawn
Copy link
Collaborator

shadowspawn commented Sep 4, 2024

Thanks for the clear description.

Commander does not support calling parse multiple times. The intended approach is you create a fresh program for each call.

See #2036 for some background and past issues.

Also with the last 12 hours, someone else reported same issue! #2246

@edmsamuel
Copy link
Author

Thank you for the clarification. I wasn't aware that Commander doesn't support multiple calls to parse. 🫣 I will definitely use a fresh program instance for each call going forward. Appreciate the guidance and reference to the related issues. 🖖🏽

@shadowspawn
Copy link
Collaborator

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

@shadowspawn
Copy link
Collaborator

Support for multiple parse added in version 13.0.0

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