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.<br>
<br>
Why exactly do you think you need to find the number of optional arguments?<br>
<br>
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.<br>
<br>
See "<a name="how-optik-handles-errors">How optik handles errors" - http://optik.sourceforge.net/doc/1.5/tutorial.html<br>
</a><br>
If you really think you need to be doing this, you can modify the following function:<br>
<br>
<pre id="CA-3f9f90a846e2f4f3f9fe9989b74cdb5704f8348c_001" dir="ltr" lang="en"><span class="line"><span class="ResWord">def</span> <span class="ID">is_empty</span><span class="Operator">(</span><span class="ID">options</span>
<span class="Operator">)</span><span class="Operator">:</span></span><br><span class="line">    <span class="String">"""</span></span><br><span class="line"><span class="String">    Returns True or False if an option is set or not.
</span></span><br><span class="line"><span class="String">    """</span></span><br><span class="line">    <span class="ID">values</span> <span class="Operator">=</span> <span class="ID">options</span><span class="Operator">
.</span><span class="ID">__dict__</span><span class="Operator">.</span><span class="ID">values</span><span class="Operator">(</span><span class="Operator">)</span></span><br><span class="line">    <span class="ResWord">return
</span> <span class="Operator">(</span><span class="ID">values</span> <span class="Operator">==</span> <span class="Operator">[</span><span class="ID">None</span><span class="Operator">]</span> <span class="Operator">*</span>
 <span class="ID">len</span><span class="Operator">(</span><span class="ID">values</span><span class="Operator">)</span><span class="Operator">)<span style="font-family: arial,sans-serif;"></span></span></span><br><br></pre>

<span><br>
On 12/4/05, Ritesh Raj Sarraf <<a href="mailto:rrs@researchut.com">rrs@researchut.com</a>> wrote:<br></span><div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
-----BEGIN PGP SIGNED MESSAGE-----<br>Hash: SHA1<br><br>Hi,<br><br>I'm using optparse module to parse all options and arguments.<br><br>My program uses mostly "option arguments" hence my len(args) value is always
<br>zero. I need to check if the user has passed the correct number of "option<br>arguments". Something like:<br><br>(options,args) = parser.parse_args()<br><br>len(options) != 1 or len(options) > 2:<br>        print "Incorrect number of arguments passed."
<br><br>How do I accomplish it ?<br><br>Regards,<br><br>rrs<br>- --<br>Ritesh Raj Sarraf<br>RESEARCHUT -- <a href="http://www.researchut.com">http://www.researchut.com</a><br>"Stealing logics from one person is plagiarism, stealing from many is
<br>research."<br>"Necessity is the mother of invention."<br><br>Note: Please CC me. I'm not subscribed to the list<br>-----BEGIN PGP SIGNATURE-----<br>Version: GnuPG v1.4.2 (GNU/Linux)<br><br>iD8DBQFDk1Nh4Rhi6gTxMLwRApx0AJ9XHlWFU1J0NdN02gtvimogUSgDkACgmkOO
<br>2pX8ocoC7pot1a8R4u2BWrY=<br>=piNo<br>-----END PGP SIGNATURE-----<br><br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div>
<br>