cmd.Cmd asking questions?

Tim Chase python.list at tim.thechases.com
Tue Nov 29 14:37:58 EST 2011


On 11/28/11 06:27, Robert Kern wrote:
> On 11/28/11 12:12 PM, Tim Chase wrote:
>> I can monkey with printing messages and using raw_input(),
>> but I'd like to know if there's a better way (such as
>> something interacting with readline for
>> text-entry-with-history-and-completion,
>
> If you import readline, then any following uses of
> raw_input() will automatically use readline. You may want to
> swap out the history when you use get_string() or confirm() so
> they don't mess up the regular Cmd history, but the basic
> functionality should work out-of-box.

I didn't realize raw_input() was so nicely overloaded.  After 
about 30 minutes of fighting with various bits of code (and 
learning that pdb's readline doesn't save/restore history), I 
tweaked up some "save the history; restore the history" wrapper 
which worked well.

>> or raw-character input for Y/N answers rather than the need
>> to hit<enter>, making it feel more uniform),
>
> I actually have a preference for needing to press enter for
> Y/N answers, too. It's distinctly *less* uniform to have some
> questions requiring an enter and some not. It can be
> unpleasantly surprising to the user, too.

After playing with it, allowing for a default Y/N value seems to 
make it a one-key selection via <enter>, but allow for 
less-surprising behavior as you detail.

Thanks for your input (no pun intended),

-tkc





More information about the Python-list mailing list