TA的每日心情 | 无聊 5 天前 |
---|
签到天数: 1644 天 [LV.Master]伴坛终老
|
用 readline, 以下是一个简单的例子- import readline
- CMD = ['foo1', 'foo2', 'bar1', 'bar2', 'exit']
- def completer(text, state):
- options = [cmd for cmd in CMD if cmd.startswith(text)]
- if state < len(options):
- return options[state]
- else:
- return None
- readline.parse_and_bind("tab: complete")
- readline.set_completer(completer)
- while True:
- cmd = raw_input('==> ')
- if cmd=='exit':
- break
- print(cmd)
复制代码 测试:- ==> <TAB><TAB>
- bar1 bar2 exit foo1 foo2
- ==> b<TAB>
- ==> bar
- ==> bar<TAB><TAB>
- bar1 bar2
- ==> bar1
- bar1
- ==> exit
复制代码 |
|