From ca7535912b1d0564654a612deed4f002a2382da8 Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Thu, 1 Sep 2022 15:32:07 +1200 Subject: samba-tool: avoid traceback for options errors What option? None yet, but see the next two commits. We use a local reference to optparse.OptionValueError, to save typing and make the eventual switch to argparse easier. Signed-off-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- python/samba/getopt.py | 3 +++ python/samba/netcmd/__init__.py | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'python') diff --git a/python/samba/getopt.py b/python/samba/getopt.py index a271cd8a736..4e70998d9d9 100644 --- a/python/samba/getopt.py +++ b/python/samba/getopt.py @@ -31,6 +31,9 @@ from samba.credentials import ( import sys +OptionError = optparse.OptionValueError + + class SambaOptions(optparse.OptionGroup): """General Samba-related command line options.""" diff --git a/python/samba/netcmd/__init__.py b/python/samba/netcmd/__init__.py index 40b9b213f70..b98f8a47fd0 100644 --- a/python/samba/netcmd/__init__.py +++ b/python/samba/netcmd/__init__.py @@ -19,7 +19,7 @@ import optparse import samba from samba import colour -from samba.getopt import SambaOption +from samba.getopt import SambaOption, OptionError from samba.logger import get_samba_logger from ldb import LdbError, ERR_INVALID_CREDENTIALS import sys @@ -110,7 +110,12 @@ class Command(object): message = "uncaught exception" force_traceback = True - if isinstance(inner_exception, LdbError): + if isinstance(e, OptionError): + print(evalue, file=self.errf) + self.usage() + force_traceback = False + + elif isinstance(inner_exception, LdbError): (ldb_ecode, ldb_emsg) = inner_exception.args if ldb_ecode == ERR_INVALID_CREDENTIALS: print("Invalid username or password", file=self.errf) -- cgit v1.2.3