aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2022-06-25 18:08:41 +0200
committerTeddy Wing2022-06-25 18:08:41 +0200
commitc175d9dc7e3df6af0905b7b597fae67539ce6c6c (patch)
tree1456ea9939018c9e689860d9544ebf4000bafe19
parenta9b373cc4798f2095117ed8e38ceb99c21a1a96d (diff)
downloadcws-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-status11
1 files changed, 10 insertions, 1 deletions
diff --git a/cws-status b/cws-status
index 8999aae..b6c1fff 100644
--- a/cws-status
+++ b/cws-status
@@ -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',