Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16322

Re: cmd.Cmd asking questions?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'mess': 0.07; 'interacting': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'surprising': 0.09; 'underlying': 0.09; 'def': 0.13; 'preference': 0.15; '(whether': 0.16; '*less*': 0.16; 'enigma': 0.16; 'kern': 0.16; 'subject:asking': 0.16; 'subject:questions': 0.16; 'wrote:': 0.18; 'exists.': 0.18; 'input': 0.22; 'header:In-Reply-To:1': 0.22; 'creating': 0.25; 'import': 0.27; 'asking': 0.28; 'not.': 0.28; 'interpret': 0.29; 'pm,': 0.29; 'class': 0.29; 'cmd': 0.30; 'subject:?': 0.31; 'header:User-Agent:1': 0.33; 'actually': 0.33; 'rather': 0.33; 'header:X-Complaints-To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'something': 0.35; 'regular': 0.35; '...': 0.36; 'uses': 0.36; 'but': 0.37; "there's": 0.37; 'using': 0.38; 'answers': 0.38; 'received:org': 0.38; 'some': 0.38; 'skip:o 20': 0.38; "i'd": 0.39; 'should': 0.39; "it's": 0.40; 'hit': 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; 'world': 0.62; 'our': 0.64; 'believe': 0.65; 'here:': 0.66; 'making': 0.67; 'needing': 0.68; 'press': 0.72; '12:12': 0.84; 'entry)': 0.84; 'swap': 0.84; 'uniform': 0.84; 'eco': 0.91; 'history,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: cmd.Cmd asking questions?
Date Mon, 28 Nov 2011 12:27:50 +0000
References <4ED37A96.2090905@tim.thechases.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 92.40.254.141.threembb.co.uk
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <4ED37A96.2090905@tim.thechases.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3092.1322483283.27778.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1322483283 news.xs4all.nl 6865 [2001:888:2000:d::a6]:58553
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16322

Show key headers only | View raw


On 11/28/11 12:12 PM, Tim Chase wrote:
> Are there best-practices for creating wizards or asking various questions
> (whether yes/no or text/numeric entry) in a cmd.Cmd class? Something like the
> imaginary confirm() and get_string() methods here:
>
> class MyCmd(cmd.Cmd):
> def do_name(self,line):
> s = get_string(prompt=line, default="Whatever")
> ...
> def do_save(self,line):
> if os.path.isfile(line):
> if not confirm("%r exists. Continue?", True): return
> self.save(line)
> def save(self, filename):
> ...
>
> 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.

> 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.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: cmd.Cmd asking questions? Robert Kern <robert.kern@gmail.com> - 2011-11-28 12:27 +0000

csiph-web