Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm a little unclear about what you need, but may be this outline of what happens when you create subparsers will help.</p> <pre><code>p=argparse.ArgumentParser() In [3]: sp=p.add_subparsers() In [4]: sp Out[4]: _SubParsersAction(option_strings=[], dest='==SUPPRESS==', nargs='A...', const=None, default=None, type=None, choices=OrderedDict(), help=None, metavar=None) In [5]: sp._parser_class Out[5]: argparse.ArgumentParser In [7]: spp=sp.add_parser('cmd') In [8]: spp Out[8]: ArgumentParser(prog='ipython cmd', usage=None, description=None, version=None, formatter_class=&lt;class 'argparse.HelpFormatter'&gt;, conflict_handler='error',add_help=True) </code></pre> <p><code>sp</code> is a positional <code>Action</code> that takes <code>choices</code> (the subparser names). It has a <code>hidden</code> attribute <code>_parser_class</code> that is the class that it uses to create the new subparsers.</p> <p><a href="https://stackoverflow.com/questions/20165843/argparse-how-to-handle-variable-number-of-arguments-nargs/20167038#20167038">Argparse: how to handle variable number of arguments (nargs=&#39;*&#39;)</a> raises the possibility of changing that <code>_parser_class</code> to produce different behavior in the subparsers. <a href="http://bugs.python.org/issue17204" rel="nofollow noreferrer">http://bugs.python.org/issue17204</a> is a related bug issue. </p> <p>If the subparser just needs an extra method (or a customized version of an existing one), there are a couple of ways to do it:</p> <ul> <li>subclass <code>ArgumentParser</code> and use the subclass for the parser (works if the parser does not use the new method).</li> <li>use the above discussion to create a subparser with the new class</li> <li>add the new method to the subparser object itself. </li> </ul> <p>Threads like <a href="https://stackoverflow.com/questions/962962/python-changing-methods-and-attributes-at-runtime">Python: changing methods and attributes at runtime</a> discuss the ins and outs of changing the methods of an instance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload