You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.7 KiB

  1. import unittest
  2. from gooey import GooeyParser
  3. from gooey.python_bindings import cmd_args
  4. from argparse import ArgumentParser
  5. class TextCommandLine(unittest.TestCase):
  6. def test_default_overwritten(self):
  7. parser = GooeyParser()
  8. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  9. parser.add_argument('arg', type=int, default=0)
  10. # Supply 1 as command line argument, check that it overwrites argparse default
  11. cmd_args.parse_cmd_args(parser, ['1'])
  12. argdefault = next(action for action in parser._actions if action.dest == 'arg').default
  13. self.assertEqual(argdefault, 1)
  14. def test_required_not_enforced(self):
  15. parser = GooeyParser()
  16. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  17. parser.add_argument('--arg', type=int, required=True)
  18. parser.add_argument('--argn', type=int, nargs='+')
  19. parser.add_argument('argp', type=int)
  20. mutex=parser.add_mutually_exclusive_group(required=True)
  21. mutex.add_argument('--one', action='store_true')
  22. mutex.add_argument('--two', action='store_true')
  23. # No error when we don't provide required arguments
  24. cmd_args.parse_cmd_args(parser)
  25. # Test that required/argn have been restored in parser
  26. argrequired = next(action for action in parser._actions if action.dest == 'arg').required
  27. self.assertEqual(argrequired, True)
  28. argnnargs = next(action for action in parser._actions if action.dest == 'argn').nargs
  29. self.assertEqual(argnnargs, '+')
  30. argpnargs = next(action for action in parser._actions if action.dest == 'argp').nargs
  31. self.assertEqual(argpnargs, None)
  32. mutexrequired = next(mutex for mutex in parser._mutually_exclusive_groups).required
  33. self.assertEqual(mutexrequired, True)
  34. def test_cmd_args_subparser(self):
  35. parser = GooeyParser()
  36. subparsers = parser.add_subparsers(dest='subparser')
  37. subparserA = subparsers.add_parser('A')
  38. subparserB = subparsers.add_parser('B')
  39. subparserA.add_argument('argA', type=int, default=0)
  40. subparserB.add_argument('argB', type=int, default=0)
  41. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  42. cmd_args.parse_cmd_args(parser, ['A', '1'])
  43. # Check that argA is overwritten but not argB
  44. subparseraction = next(action for action in parser._actions if action.dest == 'subparser')
  45. argAdefault = next(action for action in subparseraction.choices['A']._actions if action.dest == 'argA').default
  46. self.assertEqual(argAdefault, 1)
  47. argBdefault = next(action for action in subparseraction.choices['B']._actions if action.dest == 'argB').default
  48. self.assertEqual(argBdefault, 0)