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.

63 lines
2.8 KiB

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