6.6 Using cmd to create command-line applications
There are several ways to create 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 the argparse module to create applications with which a user can interact from the OS command line.
We have another 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 an example of the interaction:
] dice 5
Rolling 5 dice
] roll
[5, 6, 6, 1, 5]
]
We entered the dice 5 command to set the number of dice. After that, the roll command showed the results of rolling five dice. The help command will show the available commands.
6.6.1 Getting ready
The core feature...