diff options
author | Tim Harder <radhermit@gmail.com> | 2020-11-18 14:05:40 -0700 |
---|---|---|
committer | Tim Harder <radhermit@gmail.com> | 2020-11-18 14:09:17 -0700 |
commit | 5d818f155c7bcd980142793c9e106b9eae00f270 (patch) | |
tree | e6e15c37e90f110bc44147f0f619a2b1de404e83 /src/snakeoil/cli | |
parent | cli.tool: drop unused variable (diff) | |
download | snakeoil-5d818f155c7bcd980142793c9e106b9eae00f270.tar.gz snakeoil-5d818f155c7bcd980142793c9e106b9eae00f270.tar.bz2 snakeoil-5d818f155c7bcd980142793c9e106b9eae00f270.zip |
cli.tool: explicitly handle UserExceptions when parsing args
Instead of raising them. This makes testing directly against
parse_args() more consistent when compared to testing against a tool
instance.
Diffstat (limited to 'src/snakeoil/cli')
-rw-r--r-- | src/snakeoil/cli/tool.py | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/src/snakeoil/cli/tool.py b/src/snakeoil/cli/tool.py index 970fd471..6552b4d7 100644 --- a/src/snakeoil/cli/tool.py +++ b/src/snakeoil/cli/tool.py @@ -95,29 +95,33 @@ class Tool: :type namespace: argparse.Namespace object :param namespace: Namespace object to use for created attributes. """ - self.pre_parse(args, namespace) - options = self.parser.parse_args(args=args, namespace=namespace) - main_func = options.pop('main_func', None) - if main_func is None: - raise RuntimeError("argparser missing main method") - - # reconfigure formatters for colored output if enabled - if getattr(options, 'color', True): - formatter_factory = partial( - formatters.get_formatter, force_color=getattr(options, 'color', False)) - self.out = formatter_factory(self._outfile) - self.err = formatter_factory(self._errfile) - - # reconfigure formatters with properly parsed output verbosity - self.out.verbosity = self.err.verbosity = getattr(options, 'verbosity', 0) - - if logging.root.handlers: - # Remove the default handler. - logging.root.handlers.pop(0) - logging.root.addHandler(FormattingHandler(self.err)) - - options = self.post_parse(options) - return options, main_func + try: + self.pre_parse(args, namespace) + options = self.parser.parse_args(args=args, namespace=namespace) + main_func = options.pop('main_func', None) + if main_func is None: + raise RuntimeError("argparser missing main method") + + # reconfigure formatters for colored output if enabled + if getattr(options, 'color', True): + formatter_factory = partial( + formatters.get_formatter, force_color=getattr(options, 'color', False)) + self.out = formatter_factory(self._outfile) + self.err = formatter_factory(self._errfile) + + # reconfigure formatters with properly parsed output verbosity + self.out.verbosity = self.err.verbosity = getattr(options, 'verbosity', 0) + + if logging.root.handlers: + # Remove the default handler. + logging.root.handlers.pop(0) + logging.root.addHandler(FormattingHandler(self.err)) + + options = self.post_parse(options) + return options, main_func + except Exception as e: + # handle custom execution-related exceptions + self.handle_exec_exception(e) def pre_parse(self, args, namespace): """Handle custom options before argparsing.""" |