Skip to content

Commit a5e5282

Browse files
authored
Merge pull request #743 from python-cmd2/choices_exception
Added more validation to add_argument wrapper
2 parents f419192 + 6ab2578 commit a5e5282

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.16 (TBD, 2019)
2+
* Enhancements
3+
* Raise `TypeError` if trying to set choices/completions on argparse action that accepts no arguments
4+
15
## 0.9.15 (July 24, 2019)
26
* Bug Fixes
37
* Fixed exception caused by tab completing after an invalid subcommand was entered

cmd2/argparse_custom.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,17 @@ def _add_argument_wrapper(self, *args,
346346
new_arg = orig_actions_container_add_argument(self, *args, **kwargs)
347347

348348
# Verify consistent use of arguments
349-
choice_params = [new_arg.choices, choices_function, choices_method, completer_function, completer_method]
350-
num_set = len(choice_params) - choice_params.count(None)
349+
choices_params = [new_arg.choices, choices_function, choices_method, completer_function, completer_method]
350+
num_params_set = len(choices_params) - choices_params.count(None)
351351

352-
if num_set > 1:
353-
err_msg = ("Only one of the following may be used in an argparser argument at a time:\n"
352+
if num_params_set > 1:
353+
err_msg = ("Only one of the following parameters may be used at a time:\n"
354354
"choices, choices_function, choices_method, completer_function, completer_method")
355355
raise (ValueError(err_msg))
356+
elif num_params_set > 0 and new_arg.nargs == 0:
357+
err_msg = ("None of the following parameters can be used for this type of action:\n"
358+
"choices, choices_function, choices_method, completer_function, completer_method")
359+
raise (TypeError(err_msg))
356360

357361
# Set the custom attributes
358362
setattr(new_arg, ATTR_NARGS_RANGE, nargs_range)

tests/test_argparse_custom.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def fake_func():
3939
pass
4040

4141

42-
@pytest.mark.parametrize('args, is_valid', [
42+
@pytest.mark.parametrize('kwargs, is_valid', [
4343
({'choices': []}, True),
4444
({'choices_function': fake_func}, True),
4545
({'choices_method': fake_func}, True),
@@ -50,14 +50,27 @@ def fake_func():
5050
({'choices_method': fake_func, 'completer_function': fake_func}, False),
5151
({'choices_method': fake_func, 'completer_method': fake_func}, False),
5252
])
53-
def test_apcustom_invalid_args(args, is_valid):
53+
def test_apcustom_choices_params_count(kwargs, is_valid):
5454
parser = Cmd2ArgumentParser(prog='test')
5555
try:
56-
parser.add_argument('name', **args)
56+
parser.add_argument('name', **kwargs)
5757
assert is_valid
5858
except ValueError as ex:
5959
assert not is_valid
60-
assert 'Only one of the following may be used' in str(ex)
60+
assert 'Only one of the following parameters' in str(ex)
61+
62+
63+
@pytest.mark.parametrize('kwargs', [
64+
({'choices_function': fake_func}),
65+
({'choices_method': fake_func}),
66+
({'completer_function': fake_func}),
67+
({'completer_method': fake_func})
68+
])
69+
def test_apcustom_no_choices_when_nargs_is_0(kwargs):
70+
with pytest.raises(TypeError) as excinfo:
71+
parser = Cmd2ArgumentParser(prog='test')
72+
parser.add_argument('name', action='store_true', **kwargs)
73+
assert 'None of the following parameters can be used' in str(excinfo.value)
6174

6275

6376
def test_apcustom_usage():

0 commit comments

Comments
 (0)