option argument length

Nicolas Couture nicolas at stormvault.net
Sun Dec 4 19:38:53 EST 2005


I don't think that's actually what you want to do. Yes arguments are not to
be used directly as option arguments (otherwise why have option arguments
anyways ;-) but each option argument is usually evaluated under the
evaluation of the actual option and optparse will error on invalid use of
the options you build.

Why exactly do you think you need to find the number of optional arguments?

Everytime I thought about the need for this kind of option parsing design I
was able to find an easier way to achieve what I wanted to do.

See "How optik handles errors" -
http://optik.sourceforge.net/doc/1.5/tutorial.html

If you really think you need to be doing this, you can modify the following
function:

def is_empty(options):
    """
    Returns True or False if an option is set or not.
    """
    values = options.__dict__.values()
    return (values == [None] * len(values))


On 12/4/05, Ritesh Raj Sarraf <rrs at researchut.com> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
>
> I'm using optparse module to parse all options and arguments.
>
> My program uses mostly "option arguments" hence my len(args) value is
> always
> zero. I need to check if the user has passed the correct number of "option
> arguments". Something like:
>
> (options,args) = parser.parse_args()
>
> len(options) != 1 or len(options) > 2:
>         print "Incorrect number of arguments passed."
>
> How do I accomplish it ?
>
> Regards,
>
> rrs
> - --
> Ritesh Raj Sarraf
> RESEARCHUT -- http://www.researchut.com
> "Stealing logics from one person is plagiarism, stealing from many is
> research."
> "Necessity is the mother of invention."
>
> Note: Please CC me. I'm not subscribed to the list
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2 (GNU/Linux)
>
> iD8DBQFDk1Nh4Rhi6gTxMLwRApx0AJ9XHlWFU1J0NdN02gtvimogUSgDkACgmkOO
> 2pX8ocoC7pot1a8R4u2BWrY=
> =piNo
> -----END PGP SIGNATURE-----
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051204/e0f8443c/attachment.html>


More information about the Python-list mailing list