D
Don Low
Hi,
I'm going over a script that demonstrates the getopt function. I include
the script here:
#! /usr/bin/python
import sys, getopt, string
def help_message():
print '''options.py -- uses getopt to recognize options
Options: -h -- displays this help message
-a -- expects an argument
--file= -- expects an argument
--view -- doesn't necessarily expect an argument
--version -- displays Python version'''
sys.exit(0)
try:
options, xarguments = getopt.getopt(sys.argv[1:], 'ha', \
['file=', 'view=', 'version', 'python-version'])
except getopt.error:
print '''Error: You tried to use an unknown option or the
argument for an option that requires it was missing. Try
`options.py -h\' for more information.'''
sys.exit(0)
for a in options[:]:
if a[0] == '-h':
help_message()
for a in options[:]:
if a[0] == '-a' and a[1] != '':
print a[0]+' = '+a[1]
options.remove(a)
break
elif a[0] == '-a' and a[1] == '':
print '-a expects an argument'
sys.exit(0)
for a in options[:]:
if a[0] == '--file' and a[1] != '':
print a[0]+' = '+a[1]
options.remove(a)
break
elif a[0] == '--file' and a[1] == '':
print '--file expects an argument'
sys.exit(0)
for a in options[:]:
if a[0] == '--view' and a[1] != '':
print a[0]+' = '+a[1]
options.remove(a)
break
elif a[0] == '--view' and a[1] == '':
print '--view doesn\'t necessarily expect an argument...'
options.remove(a)
sys.exit(0)
for a in options[:]:
if a[0] == '--version':
print 'options version 0.0.001'
sys.exit(0)
for a in options[:]:
if a[0] == '--python-version':
print 'Python '+sys.version
sys.exit(0)
# END OF SCRIPT
When I execute the script with the -a option or the --view option as in:
../script_name -a myarg
It should report back:
-a = myarg
Instead it gives me:
-a expects an argument
This goes the same for the --file argument. The only one that works as
it should is the --view argument, as in:
../help2.py --view=myarg
--view = ssdt
This is because an equal sign (=) has been appended to 'view' when
getopt is called. If I add an equal sign to file (file=), it starts
working as it should too.
Can anyone explain this?
I'm going over a script that demonstrates the getopt function. I include
the script here:
#! /usr/bin/python
import sys, getopt, string
def help_message():
print '''options.py -- uses getopt to recognize options
Options: -h -- displays this help message
-a -- expects an argument
--file= -- expects an argument
--view -- doesn't necessarily expect an argument
--version -- displays Python version'''
sys.exit(0)
try:
options, xarguments = getopt.getopt(sys.argv[1:], 'ha', \
['file=', 'view=', 'version', 'python-version'])
except getopt.error:
print '''Error: You tried to use an unknown option or the
argument for an option that requires it was missing. Try
`options.py -h\' for more information.'''
sys.exit(0)
for a in options[:]:
if a[0] == '-h':
help_message()
for a in options[:]:
if a[0] == '-a' and a[1] != '':
print a[0]+' = '+a[1]
options.remove(a)
break
elif a[0] == '-a' and a[1] == '':
print '-a expects an argument'
sys.exit(0)
for a in options[:]:
if a[0] == '--file' and a[1] != '':
print a[0]+' = '+a[1]
options.remove(a)
break
elif a[0] == '--file' and a[1] == '':
print '--file expects an argument'
sys.exit(0)
for a in options[:]:
if a[0] == '--view' and a[1] != '':
print a[0]+' = '+a[1]
options.remove(a)
break
elif a[0] == '--view' and a[1] == '':
print '--view doesn\'t necessarily expect an argument...'
options.remove(a)
sys.exit(0)
for a in options[:]:
if a[0] == '--version':
print 'options version 0.0.001'
sys.exit(0)
for a in options[:]:
if a[0] == '--python-version':
print 'Python '+sys.version
sys.exit(0)
# END OF SCRIPT
When I execute the script with the -a option or the --view option as in:
../script_name -a myarg
It should report back:
-a = myarg
Instead it gives me:
-a expects an argument
This goes the same for the --file argument. The only one that works as
it should is the --view argument, as in:
../help2.py --view=myarg
--view = ssdt
This is because an equal sign (=) has been appended to 'view' when
getopt is called. If I add an equal sign to file (file=), it starts
working as it should too.
Can anyone explain this?