Re: [Tutor] Optparse question: if only certain values are acceptable
On Fri, May 8, 2009 at 9:09 PM, Terry Carroll wrote: > It's exactly what I was looking for. Thanks very much. > > Now I'm going to have to re-read the docs and see why I couldn't pick that > up from them. You have to read pretty far down, to the sections on Option attributes and Standard Option Types http://docs.python.org/library/optparse.html#option-attributes http://docs.python.org/library/optparse.html#standard-option-types Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Optparse question: if only certain values are acceptable
"Terry Carroll" wrote The toy summary is that I want to have the following command format: prognam -f FORMAT Where FORMAT, if specified, must be one of "X", "Y", or "Z". In otherwords, if the user enters: progname -f X It runs, producing its output in format X. Similar if "Y" or "Z" is specified instead of "X". But if the user specifies progname -f A I want it to spit up because A is not a recognized format. I don't see anything in the docs that directly addresses this. Is this something that I should use the callback parameter for? Which part do you not understand or do you believe is not addresssed? optparse can certainly allow you to specify the -f option and store the user entered value. Your code will have to act accordingly based on the value, optparse won't do that bit. And for the invalid option value I think you can handle that too, in an else clause. Thats the easiest way to use it IMHO. You can define a callback for your option but with only one option I dont see much point, you still need the value if/else inside the callback. You can define the list of values too (via choice) but again catching anything other than X,Y,Z is just as easy using an else clause in your case. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Optparse question: if only certain values are acceptable
On Sat, 9 May 2009, Sander Sweers wrote: > Is the below what you are looking for? It's exactly what I was looking for. Thanks very much. Now I'm going to have to re-read the docs and see why I couldn't pick that up from them. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Optparse question: if only certain values are acceptable
2009/5/9 Terry Carroll : > In otherwords, if the user enters: > > progname -f X > > It runs, producing its output in format X. Similar if "Y" or "Z" is > specified instead of "X". > > But if the user specifies > > progname -f A > > I want it to spit up because A is not a recognized format. Is the below what you are looking for? >>> import optparse >>> parser = optparse.OptionParser() >>> parser.add_option('-f', '--format', type='choice', action='store', >>> choices=('x','y','z'), dest='format') >>> args = ['-f', 'd'] #Wrong format d >>> options, restargs = parser.parse_args(args) Usage: [options] : error: option -f: invalid choice: 'd' (choose from 'x', 'y', 'z') Traceback (most recent call last): File "", line 1, in parser.parse_args(args) File "C:\Python26\lib\optparse.py", line 1382, in parse_args self.error(str(err)) File "C:\Python26\lib\optparse.py", line 1564, in error self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg)) File "C:\Python26\lib\optparse.py", line 1554, in exit sys.exit(status) SystemExit: 2 >>> args = ['-f', 'x'] #Correct format x >>> options, restargs = parser.parse_args(args) >>> options.format 'x' Greets Sander ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Optparse question: if only certain values are acceptable
I'm tryng to use optparse for the first time. The toy summary is that I want to have the following command format: prognam -f FORMAT Where FORMAT, if specified, must be one of "X", "Y", or "Z". In otherwords, if the user enters: progname -f X It runs, producing its output in format X. Similar if "Y" or "Z" is specified instead of "X". But if the user specifies progname -f A I want it to spit up because A is not a recognized format. I don't see anything in the docs that directly addresses this. Is this something that I should use the callback parameter for? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor