473,588 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Module to read input from commandline

Hi all,

I did some quick searching but I haven't found anything like I want.
It probably has to do with the terms I am searching for so if I
describe what I want then I hope someone can point me to a good
module.

I want to take input from the user at the command line. e.g.)
Would you like to continue [Y/n]: y
What is your favorite color: green
.....

You get the idea. Basic question answer stuff. It should handle
default options, case insensitivity, etc. Perhaps the module would
compare the inputted text against a regexp for validation. If the
module had an interface similar to OptParse that would be nice.

Anyone know of a decent module that handles this type of thing?
Writing from scratch would be simple but why re-invent the wheel.

Cheers,
James.
Jun 27 '08 #1
3 1430
What you're looking for is no module, it is included in the standard
python namespace.

raw_input

Use it like this:

value_a = raw_input("Plea se give a value for a: ")
# do your thing to value_a

But this belongs to the real basics, I suggest you get some reading
done on python.

GL
Jun 27 '08 #2
On Apr 13, 7:44 pm, thashoo...@fari fluset.mailexpi re.com wrote:
What you're looking for is no module, it is included in the standard
python namespace.

raw_input

Use it like this:

value_a = raw_input("Plea se give a value for a: ")
# do your thing to value_a

But this belongs to the real basics, I suggest you get some reading
done on python.

GL
Thanks for the response GL.

What I am looking for is a module to wrap raw_input so it can handle
some of the validation.

e.g.)
response = display_prompt( question, valid_responses ,
default_respons e )

or similar. valid_responses could be a tuple of regexp strings that
would compare against a raw_input and return one in the list. Ideally
I could customize the error message (for responses not in
valid_response) , etc.

I know it isn't rocket science to write it and I have something
already in place. I'd rather use a module built for the purpose now
that I have several scripts that will use the functionality.

Thanks!
James.

FYI: This is what I have so far:

from re import compile

def yes_no(question , default=None):
"""Prompt the user with a yes or no question."""
if default == "y":
question = "".join((questi on, " [Y/n]: "))
elif default == "n":
question = "".join((questi on, " [y/N]: "))
else:
question = "".join((questi on, " [y/n]: "))

valid_answer = "[yn]"
invalid_message = "Answer must be y or n"
answer = prompt(question , valid_answer, invalid_message , default)
return answer == 'y'

def prompt(question , valid_answer, invalid_message , default=None):
"""Prompt the user with a question and validate their response."""
is_valid = False;
compiled_valid_ answers = compile(valid_a nswer)
while not is_valid:
answer = raw_input(quest ion).lower()
if answer == "" and default is not None:
answer = default
is_valid = compiled_valid_ answers.match(a nswer)
if not is_valid:
print invalid_message
return answer
Jun 27 '08 #3
On Apr 13, 9:05*pm, ja...@reggieban d.com wrote:
On Apr 13, 7:44 pm, thashoo...@fari fluset.mailexpi re.com wrote:
What you're looking for is no module, it is included in the standard
python namespace.
<snip>
raw_input

Thanks for the response GL.

What I am looking for is a module to wrap raw_input so it can handle
some of the validation.
Looks like I found the module after-all:
http://blog.doughellmann.com/2008/05/pymotw-cmd.html
http://docs.python.org/lib/module-cmd.html

The cmd module does what I want and more.

Just wanted to post this in-case someone finds this through a search.

cheers,
James
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2593
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
9
13012
by: It's me | last post by:
Why do I get an "AttributeError: read" message when I do: import sys r=sys.stdin.read() ?? I've tried: r=sys.stdin.read(80)
3
1736
by: Chris_147 | last post by:
but it seems to depend on from where I start the Python shell. so I've got a module selfservicelabels.py with some variables defined, like this: BtnSave = "link=label.save" DeliveryAutomaat = "//input" This module is in the Lib directory. Now when I do import selfservicelabels
10
3756
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to be run as commands to a file with no extension. This allows other programs to use the command as an interface, and I can re-write the program in some other language
14
2063
by: ccdetail | last post by:
http://www.tiobe.com/index.htm?tiobe_index Python is the 7th most commonly used language, up from 8th. The only one gaining ground besides VB in the top 10. We're glad, our app is written in python. It's free at http://pnk.com and it is a web timesheet for project accounting
2
1503
by: Nelson | last post by:
Subject: How can I get the input stream in commandline application ? Hi guys! I have to get the input stream in my win32 commandline application. Using another words, I want to catch content of input stream file given like this: myapp.exe < data.txt How can I do it?
21
34375
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
2
1331
by: chrisber | last post by:
using the unittest module in Python 2.3.5, I've written some test code that ends up with if __name__ == "__main__": unittest.main() Since I want to run this code in various environments, I'd initially added some commandline options, e.g. to specify a configuration file like so
2
1002
by: python | last post by:
James, Check out the optparse module as well. The optparse module supercedes(?) the cmd module and offers a lot more functionality. Malcolm
0
7929
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7860
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6634
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5726
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3847
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3883
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.