diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2023-07-12 09:43:25 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2023-07-12 12:42:05 +1200 |
commit | 55422a116f5f89099fc2859a5ef57faf83eb3f67 (patch) | |
tree | 86e9b11ce693fb72973e5568449ff983de34d544 | |
parent | Pick shebang at build time, add Makefile (diff) | |
download | ssh_negotiate_term-55422a116f5f89099fc2859a5ef57faf83eb3f67.tar.gz ssh_negotiate_term-55422a116f5f89099fc2859a5ef57faf83eb3f67.zip |
Restore SSHArgumentParser wrapper
I think this is the best approach after all to catch errors from the
options parser quietly.
-rw-r--r-- | ssh_negotiate_term.py3 | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/ssh_negotiate_term.py3 b/ssh_negotiate_term.py3 index 31cb869..3c99613 100644 --- a/ssh_negotiate_term.py3 +++ b/ssh_negotiate_term.py3 @@ -13,6 +13,22 @@ import re import sys +class SSHArgumentParserError(Exception): + """ + Exception for SSHArgumentParser to throw on error. + """ + + +class SSHArgumentParser(argparse.ArgumentParser): + """ + Subclass argparse.ArgumentParser just so that errors raise exceptions + rather than exiting. + """ + + def error(self, message): + raise SSHArgumentParserError(message) + + class SSHNegotiateTerm(): """ Using a class, to encapsulate a fair bit of system state that gets injected @@ -77,7 +93,7 @@ class SSHNegotiateTerm(): return False # Parse the SSH command line to try and get the hostname - parser = argparse.ArgumentParser() + parser = SSHArgumentParser() for letter in self.SSH_OPTIONS_SWITCHES: parser.add_argument('-' + letter, action='store_true') for letter in self.SSH_OPTIONS_ARGUMENTS: @@ -88,7 +104,7 @@ class SSHNegotiateTerm(): try: args = parser.parse_args(self.argv[1:]) hostname = args.hostname - except SystemExit: + except SSHArgumentParserError: return False # If the hostname looks like an IPv4 or IPv6 address, we'll downgrade |