From cdffe7e60294e7c6ff1d0505d654a6b6d61b68c6 Mon Sep 17 00:00:00 2001 From: Christophe CHAUVET Date: Tue, 27 Aug 2013 19:31:41 +0200 Subject: [PATCH 1/5] Display better error message on unknown command, and return code 1 --- cliff/app.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cliff/app.py b/cliff/app.py index 5cbd195..2e65b6f 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -257,7 +257,11 @@ class App(object): return 0 def run_subcommand(self, argv): - subcommand = self.command_manager.find_command(argv) + try: + subcommand = self.command_manager.find_command(argv) + except ValueError as err: + LOG.error(err) + return 1 cmd_factory, cmd_name, sub_argv = subcommand cmd = cmd_factory(self, self.options) err = None From 47637469cf07c0b9c74227adb0a526e5b3867429 Mon Sep 17 00:00:00 2001 From: Christophe CHAUVET Date: Wed, 28 Aug 2013 06:26:33 +0200 Subject: [PATCH 2/5] Reraise error on debug --- cliff/app.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cliff/app.py b/cliff/app.py index 2e65b6f..52ccc1b 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -260,7 +260,10 @@ class App(object): try: subcommand = self.command_manager.find_command(argv) except ValueError as err: - LOG.error(err) + if self.options.debug: + LOG.exception(err) + else: + LOG.error(err) return 1 cmd_factory, cmd_name, sub_argv = subcommand cmd = cmd_factory(self, self.options) From 4010a5315aace3c3a33abf09580432b13b3efd07 Mon Sep 17 00:00:00 2001 From: Christophe CHAUVET Date: Wed, 28 Aug 2013 07:24:03 +0200 Subject: [PATCH 3/5] Return code 1 is already use, use code 2 instead --- cliff/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cliff/app.py b/cliff/app.py index 52ccc1b..8a59b71 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -264,7 +264,7 @@ class App(object): LOG.exception(err) else: LOG.error(err) - return 1 + return 2 cmd_factory, cmd_name, sub_argv = subcommand cmd = cmd_factory(self, self.options) err = None From e07f0bba1825e440a1ebe1cea03182225bcb8e08 Mon Sep 17 00:00:00 2001 From: Christophe CHAUVET Date: Wed, 28 Aug 2013 08:15:59 +0200 Subject: [PATCH 4/5] Add test to check if return code is 2 on unknown command --- cliff/tests/test_app.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cliff/tests/test_app.py b/cliff/tests/test_app.py index 5f13007..a879ccd 100644 --- a/cliff/tests/test_app.py +++ b/cliff/tests/test_app.py @@ -374,3 +374,8 @@ def test_error_encoding_sys(): app.stderr.write(u_data) actual = stderr.getvalue() assert data == actual + + +def test_unknown_cmd(): + app, command = make_app() + assert app.run(['hell']) == 2 From 5ba14adc41615e89020d2059b1e22c29e6220e25 Mon Sep 17 00:00:00 2001 From: Christophe CHAUVET Date: Thu, 29 Aug 2013 07:23:41 +0200 Subject: [PATCH 5/5] Re-raise Exception on debug mode --- cliff/app.py | 2 +- cliff/tests/test_app.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cliff/app.py b/cliff/app.py index 8a59b71..39ccbdd 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -261,7 +261,7 @@ class App(object): subcommand = self.command_manager.find_command(argv) except ValueError as err: if self.options.debug: - LOG.exception(err) + raise else: LOG.error(err) return 2 diff --git a/cliff/tests/test_app.py b/cliff/tests/test_app.py index a879ccd..e3bc12e 100644 --- a/cliff/tests/test_app.py +++ b/cliff/tests/test_app.py @@ -379,3 +379,11 @@ def test_error_encoding_sys(): def test_unknown_cmd(): app, command = make_app() assert app.run(['hell']) == 2 + + +def test_unknown_cmd_debug(): + app, command = make_app() + try: + app.run(['--debug', 'hell']) == 2 + except ValueError as err: + assert "['hell']" in ('%s' % err)