python cmd模块中的持久历史记录

时间:2022-09-02 13:49:24

Is there any way to configure the CMD module from Python to keep a persistent history even after the interactive shell has been closed?

有没有办法从Python配置CMD模块,即使在交互式shell关闭后仍保留持久历史记录?

When I press the up and down keys I would like to access commands that were previously entered into the shell on previous occasions that I ran the python script as well as the ones I have just entered during this session.

当我按下向上和向下键时,我想访问先前在我运行python脚本以及我刚刚在此会话期间输入的脚本时先前输入shell的命令。

If its any help cmd uses set_completer imported from the readline module

如果它的任何帮助cmd使用从readline模块导入的set_completer

1 个解决方案

#1


7  

readline automatically keeps a history of everything you enter. All you need to add is hooks to load and store that history.

readline会自动保存您输入的所有内容的历史记录。您需要添加的是挂钩以加载和存储该历史记录。

Use readline.read_history_file(filename) to read a history file. Use readline.write_history_file() to tell readline to persist the history so far. You may want to use readline.set_history_length() to keep this file from growing without bound:

使用readline.read_history_file(filename)读取历史记录文件。使用readline.write_history_file()告诉readline到目前为止保留历史记录。您可能希望使用readline.set_history_length()来保持此文件不受限制地增长:

import os.path
try:
    import readline
except ImportError:
    readline = None

histfile = os.path.expanduser('~/.someconsole_history')
histfile_size = 1000

class SomeConsole(cmd.Cmd):
    def preloop(self):
        if readline and os.path.exists(histfile):
            readline.read_history_file(histfile)

    def postloop(self):
        if readline:
            readline.set_history_length(histfile_size)
            readline.write_history_file(histfile)

I used the Cmd.preloop() and Cmd.postloop() hooks to trigger loading and saving to the points where the command loop starts and ends.

我使用Cmd.preloop()和Cmd.postloop()挂钩来触发加载并保存到命令循环开始和结束的点。

If you don't have readline installed, you could simulate this still by adding a precmd() method and record the entered commands yourself.

如果您没有安装readline,您可以通过添加precmd()方法并自行记录输入的命令来模拟这一点。

#1


7  

readline automatically keeps a history of everything you enter. All you need to add is hooks to load and store that history.

readline会自动保存您输入的所有内容的历史记录。您需要添加的是挂钩以加载和存储该历史记录。

Use readline.read_history_file(filename) to read a history file. Use readline.write_history_file() to tell readline to persist the history so far. You may want to use readline.set_history_length() to keep this file from growing without bound:

使用readline.read_history_file(filename)读取历史记录文件。使用readline.write_history_file()告诉readline到目前为止保留历史记录。您可能希望使用readline.set_history_length()来保持此文件不受限制地增长:

import os.path
try:
    import readline
except ImportError:
    readline = None

histfile = os.path.expanduser('~/.someconsole_history')
histfile_size = 1000

class SomeConsole(cmd.Cmd):
    def preloop(self):
        if readline and os.path.exists(histfile):
            readline.read_history_file(histfile)

    def postloop(self):
        if readline:
            readline.set_history_length(histfile_size)
            readline.write_history_file(histfile)

I used the Cmd.preloop() and Cmd.postloop() hooks to trigger loading and saving to the points where the command loop starts and ends.

我使用Cmd.preloop()和Cmd.postloop()挂钩来触发加载并保存到命令循环开始和结束的点。

If you don't have readline installed, you could simulate this still by adding a precmd() method and record the entered commands yourself.

如果您没有安装readline,您可以通过添加precmd()方法并自行记录输入的命令来模拟这一点。