import sy.prompt
# Ask a yes or no question
yes = sy.prompt.confirm('Do you want to add a mailserver? [y/n]: ')
if not yes:
print 'I know you want one!'
print
# Mailserver choices
mailservers = ['sendmail', 'postfix', 'qmail']
# Let the user choose a mailserver
selected = sy.prompt.choose(' Which mailserver do you want?', mailservers)
mailserver = mailservers[selected]
if mailserver == 'sendmail':
usesendmail = sy.prompt.confirm(
' Do you really want to use sendmail? [y/N]: ',
default=False)
if not usesendmail:
import sys
sys.exit(0)
# Get a number
mailsperhour = sy.prompt.ask(' How many mails are you expecting per hour?: ',
type=int)
# Check the response
domainname = sy.prompt.ask(' Whats you domainname?: ', checks=[
('^\w+\.\w+$', 'Enter a domain like foo.com')])
print
print 'Ok, setting up', mailserver, 'on', domainname, 'expecting %d m/h' % mailsperhour
This would result in sessions like this:
Do you want to add a mailserver? [y/n]: n
I know you want one!
Which mailserver do you want?
1) sendmail
2) postfix
3) qmail
Choice: first
You must use numbers
Choice: 1
Do you really want to use sendmail? [y/N]: q
Answer yes or no
Do you really want to use sendmail? [y/N]: y
How many mails are you expecting per hour?: a lot!
Answer must be a integer
How many mails are you expecting per hour?: 5
Whats you domainname?: foo,com
Enter a domain like foo.com
Whats you domainname?: foo.com
Ok, setting up sendmail on foo.com expecting 5 m/h
Question and their errorresponses can be indented which makes us able to create nicer nested prompts.
synopsis: | Prompt users for information |
---|
Ask user a question
Parameters: |
|
---|
Let user select one or more items from a list
Presents user with the question and the list of choices. Returns the index of the choice selected. If multichoice is true the user can pick more then one choice and a list of indexes are returned:
choice = sy.prompt.choose('Pick one:', ['a', 'b', 'c'])
# Pick one:
# 1) a
# 2) b
# 3) c
# Choice: 1
print choice
0
choices = sy.prompt.choose('Pick one or more:', ['a', 'b', 'c'],
mutlichoice=True)
# Pick one or more:
# 1) a
# 2) b
# 3) c
# Choices: 1, 3
print choices
[0,2]
Parameters: |
|
---|
Ask a yes or no question
Parameters: |
|
---|---|
Returns: | Boolean answer |