Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/azure/cli/_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,13 @@ def not_global(a):
print(L("Missing required argument {}".format(a)))
return ArgumentParserResult(self._display_usage(nouns, m, out))

output_format = None
if others and 'output' in others:
if others['output'] in OutputProducer.format_dict:
output_format = others['output']
del others['output']
else:
print(L("Invalid output format '{}' specified".format(others['output'])))
try:
output_format = others.pop('output') if others else None
if output_format is not None and output_format not in OutputProducer.format_dict:
print(L("Invalid output format '{}'".format(output_format)))
return ArgumentParserResult(self._display_usage(nouns, m, out))
except KeyError:
output_format = None

old_stdout = sys.stdout
try:
Expand Down
8 changes: 4 additions & 4 deletions src/azure/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def main(args, file=sys.stdout): #pylint: disable=redefined-builtin
commands.add_to_parser(parser)

try:
cmd_res = parser.execute(args)
cmd_result = parser.execute(args)
# Commands can return a dictionary/list of results
# If they do, we print the results.
if cmd_res.result:
formatter = OutputProducer.get_formatter(cmd_res.output_format)
OutputProducer(formatter=formatter, file=file).out(cmd_res.result)
if cmd_result.result:
formatter = OutputProducer.get_formatter(cmd_result.output_format)
OutputProducer(formatter=formatter, file=file).out(cmd_result.result)
except RuntimeError as ex:
logger.error(ex.args[0])
return ex.args[1] if len(ex.args) >= 2 else -1
Expand Down
68 changes: 34 additions & 34 deletions src/azure/cli/tests/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,38 @@ def test_args(self):
p = ArgumentParser('test')
p.add_command(lambda a, b: (a, b), 'n1', args=[('--arg -a', '', False), ('-b <v>', '', False)])

cmd_res = p.execute('n1 -a x'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 -a x'.split())
res, other = cmd_result.result
self.assertTrue(res.arg)
self.assertSequenceEqual(res.positional, ['x'])

# Should recognize args with alternate prefix
cmd_res = p.execute('n1 /a'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 /a'.split())
res, other = cmd_result.result
self.assertTrue(res.arg)
cmd_res = p.execute('n1 /arg'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 /arg'.split())
res, other = cmd_result.result
self.assertTrue(res.arg)

# Should not recognize "------a"
cmd_res = p.execute('n1 ------a'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 ------a'.split())
res, other = cmd_result.result
self.assertNotIn('arg', res)
# First two '--' match, so '----a' is added to dict
self.assertIn('----a', other)

cmd_res = p.execute('n1 -a:x'.split())
res = cmd_res.result
cmd_result = p.execute('n1 -a:x'.split())
res = cmd_result.result
self.assertIsNone(res)

cmd_res = p.execute('n1 -b -a x'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 -b -a x'.split())
res, other = cmd_result.result
self.assertEqual(res.b, '-a')
self.assertSequenceEqual(res.positional, ['x'])
self.assertRaises(IncorrectUsageError, lambda: res.arg)

cmd_res = p.execute('n1 -b:-a x'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 -b:-a x'.split())
res, other = cmd_result.result
self.assertEqual(res.b, '-a')
self.assertSequenceEqual(res.positional, ['x'])
self.assertRaises(IncorrectUsageError, lambda: res.arg)
Expand All @@ -86,18 +86,18 @@ def test_unexpected_args(self):
p = ArgumentParser('test')
p.add_command(lambda a, b: (a, b), 'n1', args=[('-a', '', False)])

cmd_res = p.execute('n1 -b=2'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 -b=2'.split())
res, other = cmd_result.result
self.assertFalse(res)
self.assertEqual('2', other.b)

cmd_res = p.execute('n1 -b.c.d=2'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 -b.c.d=2'.split())
res, other = cmd_result.result
self.assertFalse(res)
self.assertEqual('2', other.b.c.d)

cmd_res = p.execute('n1 -b.c.d 2 -b.c.e:3'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 -b.c.d 2 -b.c.e:3'.split())
res, other = cmd_result.result
self.assertFalse(res)
self.assertEqual('2', other.b.c.d)
self.assertEqual('3', other.b.c.e)
Expand All @@ -106,8 +106,8 @@ def test_required_args(self):
p = ArgumentParser('test')
p.add_command(lambda a, b: (a, b), 'n1', args=[('--arg -a', '', True), ('-b <v>', '', False)])

cmd_res = p.execute('n1 -a x'.split())
res, other = cmd_res.result
cmd_result = p.execute('n1 -a x'.split())
res, other = cmd_result.result
self.assertTrue(res.arg)
self.assertSequenceEqual(res.positional, ['x'])

Expand Down Expand Up @@ -144,25 +144,25 @@ def test_specify_output_format(self):
p = ArgumentParser('test')
p.add_command(lambda a, b: (a, b), 'n1', args=[('--arg -a', '', True), ('-b <v>', '', False)])

cmd_res = p.execute('n1 -a x'.split())
self.assertEqual(cmd_res.output_format, None)
cmd_result = p.execute('n1 -a x'.split())
self.assertEqual(cmd_result.output_format, None)

cmd_res = p.execute('n1 -a x --output json'.split())
self.assertEqual(cmd_res.output_format, 'json')
cmd_result = p.execute('n1 -a x --output json'.split())
self.assertEqual(cmd_result.output_format, 'json')

cmd_res = p.execute('n1 -a x --output table'.split())
self.assertEqual(cmd_res.output_format, 'table')
cmd_result = p.execute('n1 -a x --output table'.split())
self.assertEqual(cmd_result.output_format, 'table')

cmd_res = p.execute('n1 -a x --output text'.split())
self.assertEqual(cmd_res.output_format, 'text')
cmd_result = p.execute('n1 -a x --output text'.split())
self.assertEqual(cmd_result.output_format, 'text')

# Invalid format
cmd_res = p.execute('n1 -a x --output unknown'.split())
self.assertEqual(cmd_res.output_format, None)
cmd_result = p.execute('n1 -a x --output unknown'.split())
self.assertEqual(cmd_result.output_format, None)

# Invalid format
cmd_res = p.execute('n1 -a x --output'.split())
self.assertEqual(cmd_res.output_format, None)
cmd_result = p.execute('n1 -a x --output'.split())
self.assertEqual(cmd_result.output_format, None)

if __name__ == '__main__':
unittest.main()
4 changes: 2 additions & 2 deletions src/azure/cli/tests/test_autocommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def testfunc(args, _):
p = ArgumentParser('automcommandtest')
add_to_parser(p, 'test')

cmd_res = p.execute(command_name.split(' ') + '--tre wombat'.split(' '))
self.assertEqual(cmd_res.result, func)
cmd_result = p.execute(command_name.split(' ') + '--tre wombat'.split(' '))
self.assertEqual(cmd_result.result, func)

if __name__ == '__main__':
unittest.main()