From: Douglas Bagnall Date: Thu, 1 Sep 2022 03:32:07 +0000 (+1200) Subject: samba-tool: avoid traceback for options errors X-Git-Tag: talloc-2.4.0~1184 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ca7535912b1d0564654a612deed4f002a2382da8;p=thirdparty%2Fsamba.git 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 --- 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)