473,472 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to handle wrong input in getopt package in Python?

Hello all:

I am using getopt package in Python and found a problem that I can not
figure out an easy to do .

// Correct input
D:\>python AddArrowOnImage.py --imageDir=c:/

// Incorrect input
D:\>python AddArrowOnImage.py --imageDi
Traceback (most recent call last):
File "AddArrowOnImage.py", line 113, in ?
File "AddArrowOnImage.py", line 88, in main
File "getopt.pyc", line 89, in getopt
File "getopt.pyc", line 157, in do_longs
getopt.GetoptError: option --imageDir requires argument

Is there any method in getopt or Python that I could easily check the
validity of each
command line input parameter?

Thank you
-Daniel
FYI: My code that deals with getopt is as follows:

o, a = getopt.getopt(sys.argv[1:], 'h', ['imageDir='])

opts = {}
for k,v in o:
if k in ['--imageDir'] and len(v) == 0:
usage(); sys.exit("Insufficient input parameters!")
opts[k] = v
if opts.has_key('-h'):

usage(); sys.exit(0)
if not len(opts['--imageDir']):
usage(); sys.exit("Insufficient input parameters!")

Aug 25 '06 #1
1 2293

Danielgetopt.GetoptError: option --imageDir requires argument

Which is precisely what the getopt module should do.

DanielIs there any method in getopt or Python that I could easily
Danielcheck the validity of each command line input parameter?

Getopt already did the validity check for you. Just catch its exception,
display a meaningful usage message, then exit, e.g.:

try:
o, a = getopt.getopt(sys.argv[1:], 'h', ['imageDir='])
except getopt.GetoptError, msg:
usage(msg)
raise SystemExit # or something similar

My usual definition of usage() looks something like this:

def usage(msg=""):
if msg:
print >>sys.stderr, msg
print >>sys.stderr
print >sys.stderr, __doc__.strip()

This will display the module-level doc string for the user. I find that a
convenient place to document the usage of scripts. For more complex
programs where command line arg processing is in a separate module you may
have to do something like:

def usage(msg=""):
if msg:
print >>sys.stderr, msg
print >>sys.stderr
import __main__
print >sys.stderr, __main__.__doc__.strip()

Skip
Aug 25 '06 #2

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

Similar topics

6
by: David Bear | last post by:
I'm stumped. Trying to follow the docs and .. failure. here's the args >>> args '-Middwb@mainex1.asu.edu -AKHAAM@prlinux+898 -CA --D2003-08-20-09:28:13.417 -Ff -Hprlinux...
3
by: Dominik Kaspar | last post by:
I tried to use getopt and copied the example from: http://www.python.org/doc/current/lib/module-getopt.html but nothing is working... getopt.GetoptError doesn't seem to exist and when i run the...
3
by: Don Low | last post by:
Hi, I'm going over a script that demonstrates the getopt function. I include the script here: #! /usr/bin/python import sys, getopt, string
6
by: Frans Englich | last post by:
Hello, In my use of getopt.getopt, I would like to make a certain parameter mandatory. I know how to specify such that a parameter must have a value if it's specified, but I also want to make...
9
by: killermookie | last post by:
I'm not a javascripter so this is slightly unfamiliar to me. What I'm trying to do is that if someone clicks the radio button for Other, then the text input for other will become enabled. What...
4
by: Wally | last post by:
int main(int argc, char *argv) { int c; int port=0; char *srvname = NULL; extern char *optarg; /* **Determine whether we are converting a port **number to a server name or vice versa. */
3
by: KLEIN Stéphane | last post by:
Hi, I would like print tabular values on terminal (stdout). Are there package to handle table text render ? Thanks for your help, Stephane
4
by: prashant | last post by:
I am running a python script which has the line import getopt, sys, os, re, string And i get the error ImportError: No module named getopt Could you please point out a possible solution...
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.