nodeca / argparse

CLI arguments parser for node.js. JS port of python's argparse module.
Other
487 stars 73 forks source link

exit_on_error 'false' flag not respected when error in positional arguments #155

Closed aksharpatel47 closed 3 years ago

aksharpatel47 commented 3 years ago

argparse version: 2.0.1

Reproducible Code:

index.js

const argparse = require("argparse");

const parser = new argparse.ArgumentParser({ exit_on_error: false });

parser.add_argument("subCommand", {
  choices: ["api"],
});
parser.add_argument("utility", { choices: ["sub_api"], });
parser.add_argument("action", {
  choices: ["help", "create", "update", "delete"],
});

const stArgs = "api sub_api".split(
  " "
);

try {
  const args = parser.parse_args(stArgs);
} catch (error) {
  console.log("error message caught");
}

Current Output

usage: index.js [-h] {api} {sub_api} {help,create,update,delete}
index.js: error: the following arguments are required: action

The process exists after pushing the above message on the console.

Expected Output

error message caught

Looking at the source code, in the error function implementation in argparse.js, the exit_on_error flag needs to be checked before running the exit method. https://github.com/nodeca/argparse/blob/174bd80df5b45519475b9e2f611090b5b453b243/argparse.js#L3641-L3658

puzrin commented 3 years ago

https://github.com/python/cpython/blob/3.9/Lib/argparse.py#L2564 there is nothing of such kind in original.

Did you searched how they solve this issue?

aksharpatel47 commented 3 years ago

Hi @puzrin. I see they don't handle it in the original library too. To accomplish this without changing anything in this project the ArgumentParser can be extended and the error method overridden.

Feel free to close this issue and the pr if you think it is outside the scope of this library. Thanks for your time.