diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-04 11:43:26 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-04 11:43:26 -0400 |
commit | 5b4ef18ab5f74666ffa5a0125315181f0adad71c (patch) | |
tree | 84932bf69971797010dffa9f528e899778678125 | |
parent | 8f1bc02f2028ac869e61c9a88475a933046f4ee8 (diff) | |
download | cmd2-git-5b4ef18ab5f74666ffa5a0125315181f0adad71c.tar.gz |
Refactoring and updating documentation
-rw-r--r-- | cmd2/argparse_custom.py | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index 475105ec..21f5a8ab 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -66,7 +66,7 @@ def add_argument_wrapper(self, *args, :param args: arguments expected by argparse._ActionsContainer.add_argument # Customized arguments from original function - :param nargs: extends argparse nargs attribute by allowing tuples which specify a range (min, max) + :param nargs: extends argparse nargs functionality by allowing tuples which specify a range (min, max) # Added args used by AutoCompleter :param choices_function: function that provides choices for this argument @@ -88,38 +88,40 @@ def add_argument_wrapper(self, *args, :return: the created argument action """ - # pre-process special ranged nargs + # Pre-process special ranged nargs nargs_range = None - if isinstance(nargs, tuple): - if len(nargs) != 2 or not isinstance(nargs[0], int) or not isinstance(nargs[1], int): - raise ValueError('Ranged values for nargs must be a tuple of 2 integers') - if nargs[0] >= nargs[1]: - raise ValueError('Invalid nargs range. The first value must be less than the second') - if nargs[0] < 0: - raise ValueError('Negative numbers are invalid for nargs range.') - - # nargs_range is a two-item tuple (min, max) - nargs_range = nargs - - if nargs[0] == 0: - if nargs[1] > 1: - nargs_adjusted = '*' + if nargs is not None: + # Check if nargs was given as a range + if isinstance(nargs, tuple): + + # Validate nargs tuple + if len(nargs) != 2 or not isinstance(nargs[0], int) or not isinstance(nargs[1], int): + raise ValueError('Ranged values for nargs must be a tuple of 2 integers') + if nargs[0] >= nargs[1]: + raise ValueError('Invalid nargs range. The first value must be less than the second') + if nargs[0] < 0: + raise ValueError('Negative numbers are invalid for nargs range') + + # Save the nargs tuple as our range setting + nargs_range = nargs + + # Convert nargs into a format argparse recognizes + if nargs_range[0] == 0: + if nargs_range[1] > 1: + nargs_adjusted = '*' + else: + nargs_adjusted = '?' else: - # this shouldn't use a range tuple, but yet here we are - nargs_adjusted = '?' + nargs_adjusted = '+' else: - nargs_adjusted = '+' - else: - nargs_adjusted = nargs + nargs_adjusted = nargs - # Call the original add_argument function - if nargs_adjusted is not None: + # Add the argparse-recognized version of nargs to kwargs kwargs['nargs'] = nargs_adjusted - new_arg = orig_actions_container_add_argument(self, *args, **kwargs) - if nargs_range is not None: - setattr(new_arg, ATTR_NARGS_RANGE, nargs_range) + # Create the argument using the original add_argument function + new_arg = orig_actions_container_add_argument(self, *args, **kwargs) # Verify consistent use of arguments choice_params = [new_arg.choices, choices_function, choices_method, completer_function, completer_method] @@ -130,7 +132,9 @@ def add_argument_wrapper(self, *args, "choices, choices_function, choices_method, completer_function, completer_method") raise (ValueError(err_msg)) - # Set the custom attributes used by AutoCompleter + # Set the custom attributes + setattr(new_arg, ATTR_NARGS_RANGE, nargs_range) + if choices_function: setattr(new_arg, ATTR_CHOICES_CALLABLE, ChoicesCallable(is_method=False, is_completer=False, to_call=choices_function)) |