How to use the cmd2.utils.basic_complete function in cmd2

To help you get started, we’ve selected a few cmd2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-cmd2 / cmd2 / tests / test_argparse_completer.py View on Github external
def completer_function(text: str, line: str, begidx: int, endidx: int, **_kwargs) -> List[str]:
    """Tab completion function"""
    return basic_complete(text, line, begidx, endidx, completions_from_function)
github python-cmd2 / cmd2 / tests / test_completion.py View on Github external
def test_basic_completion_multiple(cmd2_app):
    text = ''
    line = 'list_food -f {}'.format(text)
    endidx = len(line)
    begidx = endidx - len(text)

    matches = sorted(utils.basic_complete(text, line, begidx, endidx, food_item_strs))
    assert matches == sorted(food_item_strs)
github python-cmd2 / cmd2 / tests / test_argparse_completer.py View on Github external
def completer_method(self, text: str, line: str, begidx: int, endidx: int, **_kwargs) -> List[str]:
        """Tab completion method"""
        return basic_complete(text, line, begidx, endidx, completions_from_method)
github python-cmd2 / cmd2 / tests / test_completion.py View on Github external
def test_basic_completion_nomatch(cmd2_app):
    text = 'q'
    line = 'list_food -f {}'.format(text)
    endidx = len(line)
    begidx = endidx - len(text)

    assert utils.basic_complete(text, line, begidx, endidx, food_item_strs) == []
github python-cmd2 / cmd2 / tests / test_argparse_completer.py View on Github external
def completer_takes_arg_tokens(text: str, line: str, begidx: int, endidx: int,
                               arg_tokens: argparse.Namespace) -> List[str]:
    """Completer function that receives arg_tokens from AutoCompleter"""
    match_against = [arg_tokens['parent_arg'][0], arg_tokens['subcommand'][0]]
    return basic_complete(text, line, begidx, endidx, match_against)
github python-cmd2 / cmd2 / cmd2 / argparse_completer.py View on Github external
def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matched_flags: List[str]) -> List[str]:
        """Tab completion routine for a parsers unused flags"""

        # Build a list of flags that can be tab completed
        match_against = []

        for flag in self._flags:
            # Make sure this flag hasn't already been used
            if flag not in matched_flags:
                # Make sure this flag isn't considered hidden
                action = self._flag_to_action[flag]
                if action.help != argparse.SUPPRESS:
                    match_against.append(flag)

        return utils.basic_complete(text, line, begidx, endidx, match_against)
github python-cmd2 / cmd2 / cmd2 / argparse_completer.py View on Github external
# If these choices are numbers, and have not yet been sorted, then sort them now
            if not self._cmd2_app.matches_sorted and all(isinstance(x, numbers.Number) for x in arg_choices):
                arg_choices.sort()
                self._cmd2_app.matches_sorted = True

            # Since choices can be various types like int, we must convert them to strings
            for index, choice in enumerate(arg_choices):
                if not isinstance(choice, str):
                    arg_choices[index] = str(choice)

            # Filter out arguments we already used
            used_values = consumed_arg_values.get(arg_action.dest, [])
            arg_choices = [choice for choice in arg_choices if choice not in used_values]

            # Do tab completion on the choices
            results = utils.basic_complete(text, line, begidx, endidx, arg_choices)

        return self._format_completions(arg_action, results)