2

Consider this minimal program that uses the cmd module:

import cmd

class Shell(cmd.Cmd):
    def do_input(self, _arg):
        '''Ask the user to input a string.'''
        s = input('String: ')
        print(s)

if __name__ == '__main__':
    Shell().cmdloop()

Here's a sample interaction with this program:

(Cmd) input
String: Hello!
Hello!
(Cmd) 

Now the problem is that upon pressing the up keyboard button, the last history item is from the user input (i.e. Hello!) rather than from the shell's prompt (i.e. input).

The question is: How can I get only the history of commands entered at the shell prompt (without the history of inputs entered using input())?

Flux
  • 9,805
  • 5
  • 46
  • 92
  • 1
    The history feature doesn't have anything to do with the `cmd` module; it's coming from the `readline` library that Python uses for `input()` when possible. You can manipulate its history list, to get rid of the unwanted item perhaps: see https://stackoverflow.com/questions/1202127/raw-input-without-leaving-a-history-in-readline for an example. – jasonharper Feb 24 '19 at 15:23

0 Answers0