summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrice Gauthier <patgauth@gmail.com>2012-05-12 17:39:00 -0400
committerPatrice Gauthier <patgauth@gmail.com>2012-05-12 17:39:00 -0400
commitcf38a177968d063fd81c989770f137e16cecc17b (patch)
tree83cdd6a3919345603e8e5232a6e5155355c276ed
parent2b5aa0b53138c5366851db22f70e9574fcbddfbb (diff)
downloaddisutils2-cf38a177968d063fd81c989770f137e16cecc17b.tar.gz
#13399: Fixes the invalid arguments handling
-rw-r--r--distutils2/run.py17
-rw-r--r--distutils2/tests/test_run.py32
2 files changed, 44 insertions, 5 deletions
diff --git a/distutils2/run.py b/distutils2/run.py
index f845dbc..1729761 100644
--- a/distutils2/run.py
+++ b/distutils2/run.py
@@ -397,8 +397,9 @@ class Dispatcher(object):
allowed = [action[0] for action in actions] + [None]
if self.action not in allowed:
- msg = 'Unrecognized action "%s"' % self.action
- raise PackagingArgError(msg)
+ msg = 'Unrecognized action %s' % self.action
+ self.show_help()
+ self.exit_with_error_msg(msg)
self._set_logger()
self.args = args
@@ -444,7 +445,8 @@ class Dispatcher(object):
try:
cmd_class = get_command_class(command)
except PackagingModuleError, msg:
- raise PackagingArgError(msg)
+ self.show_help()
+ self.exit_with_error_msg(msg)
# XXX We want to push this in distutils2.command
#
@@ -485,7 +487,11 @@ class Dispatcher(object):
cmd_class.user_options +
help_options)
parser.set_negative_aliases(_negative_opt)
- args, opts = parser.getopt(args[1:])
+ try:
+ args, opts = parser.getopt(args[1:])
+ except PackagingArgError, msg:
+ self.show_help()
+ self.exit_with_error_msg(msg)
if hasattr(opts, 'help') and opts.help:
self._show_command_help(cmd_class)
@@ -530,6 +536,9 @@ class Dispatcher(object):
def show_help(self):
self._show_help(self.parser)
+ def exit_with_error_msg(self, msg):
+ sys.exit('error: ' + msg.__str__())
+
def print_usage(self, parser):
parser.set_option_table(global_options)
diff --git a/distutils2/tests/test_run.py b/distutils2/tests/test_run.py
index 64df17f..a6e2c20 100644
--- a/distutils2/tests/test_run.py
+++ b/distutils2/tests/test_run.py
@@ -8,7 +8,7 @@ from distutils2 import install
from distutils2.tests import unittest, support
from distutils2.run import main
-from distutils2.tests.support import assert_python_ok
+from distutils2.tests.support import assert_python_ok, assert_python_failure
# setup script that uses __file__
setup_using___file__ = """\
@@ -94,6 +94,36 @@ class RunTestCase(support.TempdirManager,
self.assertTrue(build_position, out)
self.assertLess(check_position, build_position, out)
+ def test_unknown_run_option(self):
+ status, out, err = assert_python_failure(
+ '-c', 'from distutils2.run import main; main()', 'run', 'build',
+ '--unknown', PYTHONPATH=self.get_pythonpath()
+ )
+ self.assertEqual(status, 1)
+ self.assertGreater(out, '')
+ self.assertEqual(err.splitlines()[-1],
+ 'error: option --unknown not recognized')
+
+ def test_unknown_command(self):
+ status, out, err = assert_python_failure(
+ '-c', 'from distutils2.run import main; main()', 'run',
+ 'invalid_command', PYTHONPATH=self.get_pythonpath()
+ )
+ self.assertEqual(status, 1)
+ self.assertGreater(out, 1)
+ self.assertEqual(err.splitlines()[-1],
+ 'error: Invalid command invalid_command')
+
+ def test_unknown_action(self):
+ status, out, err = assert_python_failure(
+ '-c', 'from distutils2.run import main; main()', 'invalid_action',
+ PYTHONPATH=self.get_pythonpath()
+ )
+ self.assertEqual(status, 1)
+ self.assertGreater(out, 1)
+ self.assertEqual(err.splitlines()[-1],
+ 'error: Unrecognized action invalid_action')
+
# TODO test that custom commands don't break --list-commands