Using cmd to create command-line applications
There are several ways of creating interactive applications. The Using input() and getpass() for user input recipe looked at functions such as input() and getpass.getpass(). The Using argparse to get command-line input recipe showed us how to use argparse to create applications with which a user can interact from the OS command line.
We have a third way to create interactive applications: using the cmd module. This module will prompt the user for input, and then invoke a specific method of the class we provide.
Here's how the interaction will look – we've marked user input like this: "help":
A dice rolling tool. ? for help.
] help
Documented commands (type help <topic>):
========================================
dice help reroll roll
Undocumented commands:
======================
EOF quit
] help roll
Roll the dice. Use the dice command to set the number of dice.
] help dice
Sets the number...