Parse command line options

H

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:eek::',
["Number=","time=","help=","image_file=","Output 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.
 
K

Klaus Alexander Seistrup

Hue said:
try:

opts,args = getopt.getopt(sys.argv[1:], 'n:t:h:i:eek::',
["Number=","time=","help=","image_file=","Output 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,
 
T

Tim Daneliuk

hue wrote:
try:

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

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

thomasadunham

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:eek::',
["Number=","time=","help=","image_file=","Output 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top