Skip to content

Commit 394cb57

Browse files
Alistair Colessmerritt
authored andcommitted
Fix context sensitive help for info and tempurl
Make it so that swift <cmd> --help will print the info subcommand help for info and tempurl just like all the other subcommands. Also add unit tests to verify subcommand help. Change-Id: Id3666dcf72a9727fbfda2f74c23293ada1c53aa0
1 parent f9ea672 commit 394cb57

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

swiftclient/shell.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
BASENAME = 'swift'
4848
POLICY = 'X-Storage-Policy'
49+
commands = ('delete', 'download', 'list', 'post',
50+
'stat', 'upload', 'capabilities', 'info', 'tempurl')
4951

5052

5153
def get_conn(options):
@@ -1244,7 +1246,7 @@ def _upload_dir(path, object_queue, object_name):
12441246
12451247
Optional positional arguments:
12461248
<proxy_url> Proxy URL of the cluster to retrieve capabilities.
1247-
'''
1249+
'''.strip('\n')
12481250
st_info_help = st_capabilities_help
12491251

12501252

@@ -1344,6 +1346,12 @@ def parse_args(parser, args, enforce_requires=True):
13441346
args = ['-h']
13451347
(options, args) = parser.parse_args(args)
13461348

1349+
if len(args) > 1 and args[1] == '--help':
1350+
_help = globals().get('st_%s_help' % args[0],
1351+
"no help for %s" % args[0])
1352+
print(_help)
1353+
exit()
1354+
13471355
# Short circuit for tempurl, which doesn't need auth
13481356
if len(args) > 0 and args[0] == 'tempurl':
13491357
return options, args
@@ -1371,25 +1379,6 @@ def parse_args(parser, args, enforce_requires=True):
13711379
'region_name': options.os_region_name,
13721380
}
13731381

1374-
if len(args) > 1 and args[1] == '--help':
1375-
if args[0] == 'capabilities':
1376-
print(st_capabilities_help)
1377-
elif args[0] == 'delete':
1378-
print(st_delete_help)
1379-
elif args[0] == 'download':
1380-
print(st_download_help)
1381-
elif args[0] == 'list':
1382-
print(st_list_help)
1383-
elif args[0] == 'post':
1384-
print(st_post_help)
1385-
elif args[0] == 'stat':
1386-
print(st_stat_help)
1387-
elif args[0] == 'upload':
1388-
print(st_upload_help)
1389-
else:
1390-
print("no help for %s" % args[0])
1391-
exit()
1392-
13931382
if len(args) > 1 and args[0] == "capabilities":
13941383
return options, args
13951384

@@ -1588,8 +1577,6 @@ def main(arguments=None):
15881577
(options, args) = parse_args(parser, argv[1:], enforce_requires=False)
15891578
parser.enable_interspersed_args()
15901579

1591-
commands = ('delete', 'download', 'list', 'post',
1592-
'stat', 'upload', 'capabilities', 'info', 'tempurl')
15931580
if not args or args[0] not in commands:
15941581
parser.print_usage()
15951582
if args:

tests/unit/test_shell.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,25 @@ def test_capabilities(self, connection):
356356
connection.return_value.get_capabilities.return_value = {'swift': None}
357357
swiftclient.shell.main(argv)
358358
connection.return_value.get_capabilities.assert_called_with(None)
359+
360+
361+
class TestSubcommandHelp(unittest.TestCase):
362+
363+
def test_subcommand_help(self):
364+
for command in swiftclient.shell.commands:
365+
help_var = 'st_%s_help' % command
366+
self.assertTrue(help_var in vars(swiftclient.shell))
367+
out = six.StringIO()
368+
with mock.patch('sys.stdout', out):
369+
argv = ['', command, '--help']
370+
self.assertRaises(SystemExit, swiftclient.shell.main, argv)
371+
expected = vars(swiftclient.shell)[help_var]
372+
self.assertEqual(out.getvalue().strip('\n'), expected)
373+
374+
def test_no_help(self):
375+
out = six.StringIO()
376+
with mock.patch('sys.stdout', out):
377+
argv = ['', 'bad_command', '--help']
378+
self.assertRaises(SystemExit, swiftclient.shell.main, argv)
379+
expected = 'no help for bad_command'
380+
self.assertEqual(out.getvalue().strip('\n'), expected)

0 commit comments

Comments
 (0)