Exemple #1
0
    def precmd(self, statement: cmd2.Statement) -> cmd2.Statement:
        """Try to complete entered, incomplete commands based on best-fit"""
        identifier = statement.command

        # Check if entered command is a direct fit. If so, we do not do anything here.
        is_direct_match = (identifier in self.get_all_commands())\
                          or (identifier in self.macros)\
                          or (identifier in self.aliases)

        # Continue of the given command is direct match
        if is_direct_match:
            return statement

        # Otherwise, it clearly is not a registered command, macro or alias
        # We now try to find all commands that have the given input as a prefix.
        # We disregard macros and aliases, and we only auto-complete if there is no ambiguity.
        matching_commands = [
            cmd for cmd in self.get_all_commands()
            if cmd.startswith(identifier) and cmd not in self._no_shortcut
        ]
        if len(matching_commands) == 1:
            return cmd2.Statement(
                statement.args,
                raw=statement.raw,
                command=matching_commands[0],
                arg_list=statement.arg_list,
                multiline_command=statement.multiline_command,
                terminator=statement.terminator,
                suffix=statement.suffix,
                pipe_to=statement.pipe_to,
                output=statement.output,
                output_to=statement.output_to)
        else:
            return statement
Exemple #2
0
 def precmd(self, statement):
     # Pre-process the parsed command in case it looks like one of
     # our subcommands, since cmd2 does not handle multi-part
     # command names by default.
     line_parts = self._split_line(statement)
     try:
         the_cmd = self.command_manager.find_command(line_parts)
         cmd_factory, cmd_name, sub_argv = the_cmd
     except ValueError:
         # Not a plugin command
         pass
     else:
         if hasattr(statement, 'parsed'):
             # Older cmd2 uses PyParsing
             statement.parsed.command = cmd_name
             statement.parsed.args = ' '.join(sub_argv)
         else:
             # cmd2 >= 0.9.1 uses shlex and gives us a Statement.
             statement = cmd2.Statement(
                 ' '.join(sub_argv),
                 raw=statement.raw,
                 command=cmd_name,
                 arg_list=sub_argv,
                 multiline_command=statement.multiline_command,
                 terminator=statement.terminator,
                 suffix=statement.suffix,
                 pipe_to=statement.pipe_to,
                 output=statement.output,
                 output_to=statement.output_to,
             )
     return statement
Exemple #3
0
def test_statement_is_immutable():
    string = 'foo'
    statement = cmd2.Statement(string)
    assert string == statement
    assert statement.args == statement
    assert statement.raw == ''
    with pytest.raises(attr.exceptions.FrozenInstanceError):
        statement.args = 'bar'
    with pytest.raises(attr.exceptions.FrozenInstanceError):
        statement.raw = 'baz'
Exemple #4
0
def test_statement_initialization(parser):
    string = 'alias'
    statement = cmd2.Statement(string)
    assert string == statement
    assert statement.raw is None
    assert statement.command is None
    assert statement.args is None
    assert isinstance(statement.argv, list)
    assert not statement.argv
    assert statement.multiline_command is None
    assert statement.terminator is None
    assert statement.suffix is None
    assert statement.pipe_to is None
    assert statement.output is None
    assert statement.output_to is None
Exemple #5
0
def test_statement_initialization():
    string = 'alias'
    statement = cmd2.Statement(string)
    assert string == statement
    assert statement.args == statement
    assert statement.raw == ''
    assert statement.command == ''
    assert isinstance(statement.arg_list, list)
    assert not statement.arg_list
    assert isinstance(statement.argv, list)
    assert not statement.argv
    assert statement.multiline_command == ''
    assert statement.terminator == ''
    assert statement.suffix == ''
    assert isinstance(statement.pipe_to, str)
    assert not statement.pipe_to
    assert statement.output == ''
    assert statement.output_to == ''
    def precmd(self, statement):
        """Hook method executed just before the command is executed by
        :meth:`~cmd2.Cmd.onecmd` and after adding it to history.

        :param statement: subclass of str which also contains the parsed input
        :return: a potentially modified version of the input Statement object
        """
        # NOTE(mordred): The above docstring is copied in from cmd2 because
        # current cmd2 has a docstring that sphinx finds if we don't override
        # it, and it breaks sphinx.

        # Pre-process the parsed command in case it looks like one of
        # our subcommands, since cmd2 does not handle multi-part
        # command names by default.
        line_parts = self._split_line(statement)
        try:
            the_cmd = self.command_manager.find_command(line_parts)
            cmd_factory, cmd_name, sub_argv = the_cmd
        except ValueError:
            # Not a plugin command
            pass
        else:
            if hasattr(statement, 'parsed'):
                # Older cmd2 uses PyParsing
                statement.parsed.command = cmd_name
                statement.parsed.args = ' '.join(sub_argv)
            else:
                # cmd2 >= 0.9.1 uses shlex and gives us a Statement.
                statement = cmd2.Statement(
                    ' '.join(sub_argv),
                    raw=statement.raw,
                    command=cmd_name,
                    arg_list=sub_argv,
                    multiline_command=statement.multiline_command,
                    terminator=statement.terminator,
                    suffix=statement.suffix,
                    pipe_to=statement.pipe_to,
                    output=statement.output,
                    output_to=statement.output_to,
                )
        return statement