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
This commit is contained in:
Adam Spiers 2012-04-05 13:24:50 +01:00
parent 565d144611
commit ad4a04a64e
2 changed files with 50 additions and 8 deletions

View File

@ -77,7 +77,7 @@ class OpenStackComputeShell(object):
# Global arguments
parser.add_argument('-h', '--help',
action='help',
action='store_true',
help=argparse.SUPPRESS,
)
@ -273,6 +273,10 @@ class OpenStackComputeShell(object):
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 File

@ -1,5 +1,7 @@
import cStringIO
import os
import httplib2
import sys
from novaclient import exceptions
import novaclient.shell
@ -19,20 +21,56 @@ class ShellTest(utils.TestCase):
}
_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)