<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#330099">
Filip Gruszczyński wrote:
<blockquote
cite="mid1be78d220910230308x36b1311dqf556825244740bae@mail.gmail.com"
type="cite">
<pre wrap="">optparse module is quite smart, when it comes to validating options,
like assuring, that certain option must be an integer. However, I
can't find any information about validating, that positional arguments
were provided and I can't find methods, that would allow defining
positional argument in OptionParser. Is it possible to do this, or was
it intentionaly left this way?
</pre>
</blockquote>
<tt>You can't, unlike argparse.<br>
<br>
"</tt><br>
<ul class="simple">
<li><b>The argparse module can handle positional and optional
arguments, while optparse can handle only optional arguments</b>. (See <a
title="add_argument" class="reference external"
href="http://argparse.googlecode.com/svn/tags/r101/doc/add_argument.html#add_argument"><tt
class="xref docutils literal"><span class="pre">add_argument()</span></tt></a>.)</li>
<li>The argparse module isn’t dogmatic about what your command line
interface should look like - options like <tt class="docutils literal"><span
class="pre">-file</span></tt> or <tt class="docutils literal"><span
class="pre">/file</span></tt> are supported, as are required options.
Optparse refuses to support these features, preferring purity over
practicality.</li>
<li>The argparse module produces more informative usage messages,
including command-line usage determined from your arguments, and help
messages for both positional and optional arguments. The optparse
module requires you to write your own usage string, and has no way to
display help for positional arguments.</li>
<li>The argparse module supports action that consume a variable
number
of command-line args, while optparse requires that the exact number of
arguments (e.g. 1, 2, or 3) be known in advance. (See <a
title="add_argument" class="reference external"
href="http://argparse.googlecode.com/svn/tags/r101/doc/add_argument.html#add_argument"><tt
class="xref docutils literal"><span class="pre">add_argument()</span></tt></a>.)</li>
<li>The argparse module supports parsers that dispatch to
sub-commands, while optparse requires setting <tt
class="docutils literal"><span class="pre">allow_interspersed_args</span></tt>
and doing the parser dispatch manually. (See <a title="add_subparsers"
class="reference external"
href="http://argparse.googlecode.com/svn/tags/r101/doc/other-methods.html#add_subparsers"><tt
class="xref docutils literal"><span class="pre">add_subparsers()</span></tt></a>.)</li>
<li>The argparse module allows the <tt class="docutils literal"><span
class="pre">type</span></tt> and <tt class="docutils literal"><span
class="pre">action</span></tt> parameters to <a title="add_argument"
class="reference external"
href="http://argparse.googlecode.com/svn/tags/r101/doc/add_argument.html#add_argument"><tt
class="xref docutils literal"><span class="pre">add_argument()</span></tt></a>
to be specified with simple callables, while optparse requires hacking
class attributes like <tt class="docutils literal"><span class="pre">STORE_ACTIONS</span></tt>
or <tt class="docutils literal"><span class="pre">CHECK_METHODS</span></tt>
to get proper argument checking. (See <a title="add_argument"
class="reference external"
href="http://argparse.googlecode.com/svn/tags/r101/doc/add_argument.html#add_argument"><tt
class="xref docutils literal"><span class="pre">add_argument()</span></tt></a>).</li>
<li>"</li>
</ul>
That being said, I still stick with optparse. I prefer the dogmatic
interface that makes all my exe use the exact same (POSIX) convention.
I really don't care about writing /file instead of --file<br>
<br>
JM<br>
</body>
</html>