How to generate error when argument are not supplied and there is noexplicit defults (in optparse)?

P

Peng Yu

Hi,

The following code doesn't give me error, even I don't specify the
value of filename from the command line arguments. filename gets
'None'. I checked the manual, but I don't see a way to let
OptionParser fail if an argument's value (which has no default
explicitly specified) is not specified. I may miss some thing in the
manual. Could any expert let me know if there is a way to do so?
Thanks!

#!/usr/bin/env python

from optparse import OptionParser

usage = 'usage: %prog [options] arg1 arg2'
parser = OptionParser(usage=usage)
parser.set_defaults(verbose=True)
parser.add_option('-f', '--filename')

#(options, args) = parser.parse_args(['-f', 'file.txt'])
(options, args) = parser.parse_args()

print options.filename
 
M

Miki Tebeka

Don't specify it as an option, but as an argument.
If you're on a new version of python, you should probably use argparse.
 
M

Miki Tebeka

Don't specify it as an option, but as an argument.
If you're on a new version of python, you should probably use argparse.
 
R

rurpy

Hi,

The following code doesn't give me error, even I don't specify the
value of filename from the command line arguments. filename gets
'None'. I checked the manual, but I don't see a way to let
OptionParser fail if an argument's value (which has no default
explicitly specified) is not specified. I may miss some thing in the
manual. Could any expert let me know if there is a way to do so?
Thanks!

#!/usr/bin/env python

from optparse import OptionParser

usage = 'usage: %prog [options] arg1 arg2'
parser = OptionParser(usage=usage)
parser.set_defaults(verbose=True)
parser.add_option('-f', '--filename')

#(options, args) = parser.parse_args(['-f', 'file.txt'])
(options, args) = parser.parse_args()

print options.filename

You can check it yourself.
I find I use a pretty standard pattern with optparse:

def main (args, opts):
...

def parse_cmdline ():
p = OptionParser()
p.add_option('-f', '--filename')
options, args = parser.parse_args()

if not options.filename:
p.error ("-f option required")
if len (args) != 2:
p.error ("Expected exactly 2 arguments")
# Other checks can obviously be done here too.

return args, options

if __name__ == '__main__':
args, opts = parse_cmdline()
main (args, opts)

While one can probably subclass OptionParser or use callbacks
to achieve the same end, I find the above approach simple and
easy to follow.

I also presume you know that you have can optparse produce a
usage message by adding 'help' arguments to the add_option()
calls?

And as was mentioned in another post, argparse in Python 2.7
(or in earlier Pythons by downloading/installing it yourself)
can do the checking you want.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top