-2

I am working on a small command line application based on the Cmd Python module. I can't see how to show the last command in the console prompt so that it can be edited by the user and re-submitted.

The question is not about how to store the last command. It is about how to shows it in the prompt and allow the user to edit it

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84

2 Answers2

1

From the docs for cmd, Cmd has a parameter:

Cmd.use_rawinput A flag, defaulting to true. If true, cmdloop() uses raw_input() to display a prompt and read the next command; if false, sys.stdout.write() and sys.stdin.readline() are used. (This means that by importing readline, on systems that support it, the interpreter will automatically support Emacs-like line editing and command-history keystrokes.)

I.e., you can use the up arrow to bring up the last command:

>>> import cmd
>>> C = cmd.Cmd()
>>> C.cmdloop()
(Cmd) command1
*** Unknown syntax: command1
(Cmd) command1

In the final line above, I pressed the up arrow and it remembered my last command.

maxymoo
  • 35,286
  • 11
  • 92
  • 119
0

Just use readline library. You can find example in python doc: https://docs.python.org/3/library/readline.html

Eriks Dobelis
  • 913
  • 7
  • 16