import readline as rl def setup_readline(): if 'libedit' in rl.__doc__: rl.parse_and_bind('bind ^I rl_complete') else: rl.parse_and_bind('tab: complete') def repl(): print( 'This script demonstrates an issue with the readline module.', 'Enter arbitrary text into stdin to be added to the input history.', 'Lines in all caps aren\'t supposed to be added to the history.', 'Available commands include \'history\' and \'quit\', \'enable\', and' ' \'disable\'.', 'Upon termination of this REPL, the history will be displayed.', 'Note that non-empty lines are being added twice.', sep='\n' ) try: while True: line = input('--> ') if line.isupper(): print('ERROR: Is your caps lock on?') elif line == 'history': display_history() rl.add_history(line) elif line == 'enable': rl.set_auto_history(True) elif line == 'disable': rl.set_auto_history(False) elif line == 'quit': break else: rl.add_history(line) except EOFError: print() def _get_history(): for i in range(1, rl.get_current_history_length() + 1): yield rl.get_history_item(i) def display_history(): for index, line in enumerate(_get_history(), start=1): print(index, ': ', line, sep='') if __name__ == '__main__': setup_readline() repl()