optparse options

Mike Kazantsev mike_kazantsev at fraggod.net
Wed May 20 03:54:52 EDT 2009


Ben Finney wrote:
> icarus <rsarpi at gmail.com> writes:
> 
>> 	parser = optparse.OptionParser(usage="%prog [-p dir] [--part=dir] ",
>> version="%prog 1.0")
>>
>> 	parser.add_option( "-p", "--part", dest="directory",
>> 	                  help="process target directory", metavar="dir")
>> 	(options, args) = parser.parse_args()

...

>> 	if len(args) != 1:
>> 		parser.error("No options specified")
> 
> The message is confusing, since it doesn't match the condition; it would
> be correct to say “Did not specify exactly one non-option argument”.
> 
> In this case, it looks like you don't want to check this at all, and
> should instead operate on the basis of the options only.

I also wanted to note that it looks quite illogical and
counter-intuitive to create "required options", since by definition they
should be optional.
Try using arguments instead, with some type-switching flags, if
necessary - it should make CLI more consistent and save some typing by
omitting otherwise always-required option argument ("--part").

-- 
Mike Kazantsev // fraggod.net

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 205 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20090520/e194b018/attachment.sig>


More information about the Python-list mailing list