Skip to content
Browse files

Make '--help' argument more useful.

With no other arguments, '--help' now outputs the same as 'help', so
that the list of subcommands are no longer omitted.  Additionally,
'$subcommand --help' now yields the same output as 'help $subcommand'.

Change-Id: Iabd926574e296ad14b622862b9fba038fdede66e
  • Loading branch information...
1 parent 565d144 commit ad4a04a64e3a49c5613e766a9f93727cd6b8a678 @aspiers aspiers committed Apr 5, 2012
Showing with 50 additions and 8 deletions.
  1. +5 −1 novaclient/shell.py
  2. +45 −7 tests/test_shell.py
View
6 novaclient/shell.py
@@ -77,7 +77,7 @@ def get_base_parser(self):
# Global arguments
parser.add_argument('-h', '--help',
- action='help',
+ action='store_true',
help=argparse.SUPPRESS,
)
@@ -273,6 +273,10 @@ def main(self, argv):
subcommand_parser = self.get_subcommand_parser(options.version)
self.parser = subcommand_parser
+ if options.help and len(args) == 0:
+ subcommand_parser.print_help()
+ return 0
+
args = subcommand_parser.parse_args(argv)
self._run_extension_hooks('__post_parse_args__', args)
View
52 tests/test_shell.py
@@ -1,5 +1,7 @@
+import cStringIO
import os
import httplib2
+import sys
from novaclient import exceptions
import novaclient.shell
@@ -19,20 +21,56 @@ def setUp(self):
}
_old_env, os.environ = os.environ, fake_env.copy()
- # Make a fake shell object, a helping wrapper to call it, and a quick
- # way of asserting that certain API calls were made.
- global shell, _shell, assert_called, assert_called_anytime
- _shell = novaclient.shell.OpenStackComputeShell()
- shell = lambda cmd: _shell.main(cmd.split())
+ def shell(self, argstr):
+ orig = sys.stdout
+ try:
+ sys.stdout = cStringIO.StringIO()
+ _shell = novaclient.shell.OpenStackComputeShell()
+ _shell.main(argstr.split())
+ except SystemExit:
+ exc_type, exc_value, exc_traceback = sys.exc_info()
+ self.assertEqual(exc_value.code, 0)
+ finally:
+ out = sys.stdout.getvalue()
+ sys.stdout.close()
+ sys.stdout = orig
+
+ return out
def tearDown(self):
global _old_env
os.environ = _old_env
def test_help_unknown_command(self):
- self.assertRaises(exceptions.CommandError, shell, 'help foofoo')
+ self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')
def test_debug(self):
httplib2.debuglevel = 0
- shell('--debug help')
+ self.shell('--debug help')
assert httplib2.debuglevel == 1
+
+ def test_help(self):
+ required = [
+ '^usage: ',
+ '(?m)^\s+root-password\s+Change the root password',
+ '(?m)^See "nova help COMMAND" for help on a specific command',
+ ]
+ for argstr in ['--help', 'help']:
+ help_text = self.shell(argstr)
+ for r in required:
+ self.assertRegexpMatches(help_text, r)
+
+ def test_help_on_subcommand(self):
+ required = [
+ '^usage: nova root-password',
+ '(?m)^Change the root password',
+ '(?m)^Positional arguments:',
+ ]
+ argstrings = [
+ 'root-password --help',
+ 'help root-password',
+ ]
+ for argstr in argstrings:
+ help_text = self.shell(argstr)
+ for r in required:
+ self.assertRegexpMatches(help_text, r)

0 comments on commit ad4a04a

Please sign in to comment.
Something went wrong with that request. Please try again.