Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In an attempt to solve this problem from the outside in - I have an initial implementation that patches the optparse module by replacing the OptionParser class with a subclass of OptionParser and overloads the parse_args method + exposes a new delayed_parse_args method. I'm sharing snippets from this solution in case anyone else finds its useful or can make an improvement to it.</p> <p><strong>optparse_patch.py</strong></p> <pre><code>import optparse def patch_opt_parser(): optparse.stashed_parsers = {} class OptionParserEx(optparse.OptionParser): def delayed_parse_args(self, callback): optparse.stashed_parsers[self] = callback def parse_args(self, args=None, values=None): for parser, callback in getattr(optparse,"stashed_parsers").items(): # add all the option_list &amp; option_groups from the individual # parsers in stashed_parsers to `self` for parser, callback in getattr(optparse,"stashed_parsers").items(): # update `parser` to inherit the option_lists and option_groups from # self. then .... _o, _a = optparse._OptionParser.parse_args( parser, args, values ) callback( _o, _a t) return getattr(optparse,"_OptionParser").parse_args(self, args, values) optparse._OptionParser = optparse.OptionParser optparse.OptionParser = OptionParserEx patch_opt_parser() </code></pre> <p>This allows submodules to 'stash' their expected options and have the command line options evaluated at a later stage when the client of the module actually provides an OptionParser and calls the parse_args method on it's own OptionParser. An example of such a use case is as follows:</p> <p><strong>module.py</strong></p> <pre><code>import optparse_patch import optparse parser = optparse.OptionParser() group = optparse.OptionGroup(parser, "module options") group.add_option("-f", dest="flip", action="store_true") parser.add_option_group(group) def cli_callback ( opts, args ): if opts.flip: print "flip" opts, args = parser.delayed_parse_args ( cli_callback ) </code></pre> <p><strong>main.py</strong></p> <pre><code>import module import optparse myparser = optparse.OptionParser() mygroup = optparse.OptionGroup(myparser, "main options") mygroup.add_option("-j", dest="jump", action="store_true") myparser.add_option_group(mygroup) opts, args = myparser.parse_args() if opts.jump: print "jump" </code></pre> <p>calling the program main.py results in the following outputs:</p> <p><em>python main.py --help</em></p> <pre><code>Usage: main.py [options] Options: -h, --help show this help message and exit module options: -f main options: -j </code></pre> <p><em>python main.py -j -f</em></p> <pre><code>flip jump </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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