optparser: how to register callback to display binary's help

Michele Simionato michele.simionato at gmail.com
Wed Oct 13 08:18:22 EDT 2010


Here is a solution using plac (http://pypi.python.org/pypi/plac) and
not OptionParse, in the case
the Linux underlying command is grep:

import subprocess
import plac

@plac.annotations(help=('show help', 'flag', 'h'))
def main(help):
    if help:
        script_usage = plac.parser_from(main).format_help()
        po = subprocess.Popen(['grep', '-h'], stderr=subprocess.PIPE)
        grep_usage = po.stderr.read()
        print script_usage
        print grep_usage
main.add_help = False

if __name__ == '__main__':
    plac.call(main)



More information about the Python-list mailing list