Why does Python argparse use error code 2 for SystemExit?

When I provide the Python input argparse, I don’t like it, it calls SystemExit with code 2, which means "There is no such file or directory . " Why use this error code?

import argparse import errno parser = argparse.ArgumentParser() parser.add_argument('arg') try: parser.parse_args([]) except SystemExit as se: print("Got error code {} which is {} in errno" .format(se.code, errno.errorcode[se.code])) 

produces this conclusion:

 usage: se.py [-h] arg se.py: error: too few arguments Got error code 2 which is ENOENT in errno 
+1
python errno argparse
source share
1 answer

You are misleading C errno error codes with the exit status process; these two concepts are not completely related.

Exit status values do not have an official standard , but agreement 2 implies misuse; this is the exit code used by Bash's built-in functions , for example.

The os module provides the os.EX_* constants, which represent the sysexit.h constants used by many POSIX systems. os.EX_USAGE is an exit code of 64 and can also be used, although argparse does not actually use this constant, since it is available only on UNIX systems, and argparse should work on both Windows and other platforms.

+8
source share

All Articles