469,903 Members | 1,637 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Parse command line options

hue
I am trying to parse command line options using getopt module.

I have written the following Code

import string

import getopt

def usage():
print '''haarp_make.py -- uses getopt to recognize options

Options: -n -- No
-t -- Time
-h -- help
-i -- image_file
-o -- Output:filename'''

sys.exit(1)

def main():
try:

opts,args = getopt.getopt(sys.argv[1:], 'n:t:h:i:o:',
["Number=","time=","help=","image_file=","Outpu t Filename="])

except getopt.GetoptError:
print 'Unrecognized argument or option'
usage()
sys.exit(0)

I have gone through getopt module from the help in python Library, but
i couldnot proceed from here.

My Task is

python MyScriptName.py n t h i o

How to parse those arguments, i is an Image File.
Please try to give some comments.
Hoping for a reply.

Jul 19 '05 #1
5 3302
Hue wrote:
try:

opts,args = getopt.getopt(sys.argv[1:], 'n:t:h:i:o:',
["Number=","time=","help=","image_file=","Outpu t Filename="])

except getopt.GetoptError:
print 'Unrecognized argument or option'
usage()
sys.exit(0)


Proceed with e.g.:

#v+

for (opt, arg) in opts:
if opt in ('-n', '--No'):
do_no()
elif opt in ('-t', '--Time'):
do_time()
# [...]
else:
barf(opt, arg)
# end if
# end for

#v-

But be consistent when using long options: use all lowercase, use either
dashes or underscores (not both), don't use whitespace.

I usually do something like:

#v+

Options = {
'i:': 'input=',
'o:': 'output=',
'c' : 'copyright',
'h' : 'help',
'v' : 'version',
}
shortOpts = ''.join(Options.keys())
longOpts = Options.values()

# [...]

try:
(opts, args) = getopt.getopt(argv[1:], shortOpts, longOpts)
except getopt.error, msg:
die(msg)
# end try

# [...]

for (opt, arg) in opts:
# Handle all options
:
# end for

#v-

Additional, non-option, arguments will be in the "args" variable.

Cheers,

--
Klaus Alexander Seistrup
Magnetic Ink, Copenhagen, Denmark
http://magnetic-ink.dk/
Jul 19 '05 #2
hue wrote:
<SNIP>
try:

opts,args = getopt.getopt(sys.argv[1:], 'n:t:h:i:o:',


^^^^^^^
This may be the problem. As I recall, a colon following an option
indicates that it is followed by an argument as in "-f filename".
For options that are just switches (that take no argument), I believe
they should appear in the list above *without* the colon suffix...


--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jul 19 '05 #3
Hue,
It looks like you may have options and arguments confused. Arguments
are the pieces of information that your program needs to run. Options
are used to tune the behaviour of your program.

For example:
grep -i foo bar
Tries to match the expression "foo" in the file "bar", ignoring the
case of the letters. The grep program needs to know what expression to
look for and where to look, so "foo" and "bar" are arguments. Ignoring
the case of the letters is tuning the behaviour of the grep program -
in this case making it less sensitive. The program will run without it.
If your task is :
python MyScriptName.py n t h i o
Then n, t, h, i and o are arguments. You can get them from the args
array:

import sys
print ",".join(sys.argv)

would print "n, t, h, i, o"

If you are using options, you must put dashes before them, and they
must be before the arguments. So, if this was hue.py:

import sys
import string
import getopt

def usage():
print '''haarp_make.py -- uses getopt to recognize options

Options: -n -- No
-t -- Time
-h -- help
-i -- image_file
-o -- Output:filename'''

sys.exit(1)

def main():

print "SYS ARGV: ", ",".join(sys.argv)
try:

opts,args = getopt.getopt(sys.argv[1:], 'n:t:h:i:o:',
["Number=","time=","help=","image_file=","Outpu t Filename="])

except getopt.GetoptError:
print 'Unrecognized argument or option'
usage()
sys.exit(0)

print "OPTS: ", ",".join([repr(o) for o in opts])
print "ARGS: ", ",".join(args)

if __name__ == "__main__":
main()
Then:
C:\temp>python hue.py n t h i o
SYS ARGV: hue.py,n,t,h,i,o
OPTS:
ARGS: n,t,h,i,o

And:
C:\temp>python hue.py -i image.py n t h o
SYS ARGV: hue.py,-i,image.py,n,t,h,o
OPTS: ('-i', 'image.py')
ARGS: n,t,h,o

Hope that hepls.
Tom

Jul 19 '05 #4
hue
Thanks for your reply, I have used some of your ideas in my Code.

Jul 19 '05 #5
hue
Thanks for your reply, I have used some of your ideas in my Code.

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Edvard Majakari | last post: by
6 posts views Thread by Ian Davies | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.