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

H

hiral

Hi,

I want to display help message of python script and then display help
message from the binary file (which also supports -h option):

Assumptions:
1) 'mybinary' - is linux executable file which supports '-h' and on '-
h' option it displays the help message
2) myscript.py - when passing '-h' option want to call 'mybinary -h'
and display help messages of both script and binary.

Code:
====
from optparse import OptionParser
[...]
parser = OptionParser()
parser.add_option("-e", "--execute", dest="file",
help="Execute binary", metavar="FILE")
(options, args) = parser.parse_args()
if options.file:
subprocess.call(options.file)


Requirement:
$ python myscript.py -h
<currently it prints the help message with '-e' and '-h' help message>
<becides I want to display help message of 'mybinary'>

Thank you in advance.
-Hiral
 
M

Michele Simionato

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)
 
J

Jean-Michel Pichavant

hiral said:
Hi,

I want to display help message of python script and then display help
message from the binary file (which also supports -h option):

Assumptions:
1) 'mybinary' - is linux executable file which supports '-h' and on '-
h' option it displays the help message
2) myscript.py - when passing '-h' option want to call 'mybinary -h'
and display help messages of both script and binary.

Code:
====
from optparse import OptionParser
[...]
parser = OptionParser()
parser.add_option("-e", "--execute", dest="file",
help="Execute binary", metavar="FILE")
(options, args) = parser.parse_args()
if options.file:
subprocess.call(options.file)


Requirement:
$ python myscript.py -h
<currently it prints the help message with '-e' and '-h' help message>
<becides I want to display help message of 'mybinary'>

Thank you in advance.
-Hiral

Hi,

Try something like

def myHelp(option, opt, value, parser):
parser.print_help()
if hasattr(parser.values, 'file') and parser.values.file:
proc = subprocess.Popen([parser.values.file, '-h'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print proc.stdout.read()
print proc.stderr.read()

parser.remove_option('-h')
parser.add_option("-h", "--help",
help="display the help message", action='callback',
callback = myHelp)


Jean-Michel
 
H

hiral

hiral said:
I want to display help message of python script and then display help
message from the binary file (which also supports -h option):
Assumptions:
1) 'mybinary' - is linux executable file which supports '-h' and on '-
h' option it displays the help message
2) myscript.py - when passing '-h' option want to call 'mybinary -h'
and display help messages of both script and binary.
Code:
====
from optparse import OptionParser
[...]
parser = OptionParser()
parser.add_option("-e", "--execute", dest="file",
                  help="Execute binary", metavar="FILE")
(options, args) = parser.parse_args()
if options.file:
    subprocess.call(options.file)
Requirement:
$ python myscript.py -h
<currently it prints the help message with '-e' and '-h' help message>
<becides I want to display help message of 'mybinary'>
Thank you in advance.
-Hiral

Hi,

Try something like

def myHelp(option, opt, value, parser):
    parser.print_help()
    if hasattr(parser.values, 'file') and parser.values.file:
        proc = subprocess.Popen([parser.values.file, '-h'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        print proc.stdout.read()
        print proc.stderr.read()

parser.remove_option('-h')
parser.add_option("-h", "--help",
                  help="display the help message", action='callback',
callback = myHelp)

Jean-Michel- Hide quoted text -

- Show quoted text -

Hi Jean-Michel,

It helped !!!

Thank you.
-Hiral
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top