diff options
| author | Teddy Wing | 2022-06-25 18:08:41 +0200 |
|---|---|---|
| committer | Teddy Wing | 2022-06-25 18:08:41 +0200 |
| commit | c175d9dc7e3df6af0905b7b597fae67539ce6c6c (patch) | |
| tree | 1456ea9939018c9e689860d9544ebf4000bafe19 | |
| parent | a9b373cc4798f2095117ed8e38ceb99c21a1a96d (diff) | |
| download | cws-status-c175d9dc7e3df6af0905b7b597fae67539ce6c6c.tar.bz2 | |
cws-status: Exit with code `EX_USAGE` on argument parse errorv0.0.1
I tried catching the `argparse.ArgumentError` as described here, but
that didn't seem to work:
https://docs.python.org/3.9/library/argparse.html#exit-on-error
When I wrapped `args = argparser.parse_args()` in a `try` block, it
raised a `SystemExit` exception instead of `argparse.ArgumentError`.
Take inspiration from some of the suggestions in this Stack Overflow
answer:
https://stackoverflow.com/questions/5943249/python-argparse-and-controlling-overriding-the-exit-status-code
Since we just want to change the exit code, call
`argparse.ArgumentParser`'s `error()` method and exit with the error
code we want.
| -rw-r--r-- | cws-status | 11 |
1 files changed, 10 insertions, 1 deletions
@@ -35,8 +35,17 @@ EX_USAGE = 64 EX_NOUSER = 67 +class ArgumentParser(argparse.ArgumentParser): + + def error(self, message): + try: + super().error(message) + except SystemExit: + sys.exit(EX_USAGE) + + # Parse arguments. -argparser = argparse.ArgumentParser(description='Query the publish status of \ +argparser = ArgumentParser(description='Query the publish status of \ extensions on the Chrome Web Store dashboard.') argparser.add_argument( '--url', |
