argparse icon indicating copy to clipboard operation
argparse copied to clipboard

Allow to customize the exit code

Open SirNickolas opened this issue 2 years ago • 1 comments

Currently, if a parsing error occurs, the program exits with code 1; this is hard-coded into the library. However, the program may want to use return code 1 itself and leave another code for the CLI framework. If I understood correctly, to do this, we have to invoke the lower-level parseArgs manually, check if it returned 1, and if so, distinguish it from the program’s 1 somehow (e.g., by checking a global variable). It feels really hacky for such simple task.

A blog post explaining why having different error codes is important.

SirNickolas avatar Nov 18 '23 06:11 SirNickolas

CLI!(config, COMMAND).main!newMain is very simple - it does only this:

int main(string[] argv)
{
    return CLI!(config, COMMAND).parseArgs!(newMain)(argv[1..$]);
}

So what you can do to exit with your custom code is this:

int main(string[] argv)
{
    return CLI!(config, COMMAND).parseArgs!(newMain)(argv[1..$]) ? 123 : 0;    // exit with 123 in case of error
}

andrey-zherikov avatar Jan 13 '24 04:01 andrey-zherikov