| 128 | self.assertEqual(stdout, "required: test1, optional: test2, root_default: some_value") |
| 129 | |
| 130 | def test_invoke_required(self): |
| 131 | # Test invoking a command without a required flag. |
| 132 | args = _parse([self.root_module, "child1", "grandchild1", "command1"]) |
| 133 | self.assertEqual( |
| 134 | args.argv, |
| 135 | [self.root_module, "--undefok=root,root_default,child1", "--root_default=some_value"], |
| 136 | ) |
| 137 | returncode, _, stderr = _run(args) |
| 138 | self.assertEqual(returncode, 1) |
| 139 | self.assertIn("Flag --required must have a value other than None", stderr) |
| 140 | |
| 141 | # Test invoking a command without a required command group flag (--child2). |
| 142 | with mock.patch("sys.stderr", new=StringIO()) as stderr: |
| 143 | with self.assertRaises(SystemExit) as e: |
| 144 | _parse([self.root_module, "child2", "command2", "--required=test"]) |
| 145 | self.assertEqual(e.exception.code, 2) |
| 146 | self.assertIn("the following arguments are required: --child2", stderr.getvalue()) |
| 147 | |
| 148 | def test_invoke_unknown(self): |
| 149 | # Test invoking a command with an unknown flag. |