Python options and ROOT options

Hello Duc and Wim,

from Wims last post it is obvious, that one can prevent ROOT from intercepting the command line arguments, by hiding them during importing, like so:

import sys
tmpargv = sys.argv
sys.argv = [ '-b','-n' ]
#sys.argv=[] #also works, of course
import ROOT
sys.argv = tmpargv
from optparse import OptionParser

Then the Optionparser works as expected, and shows its help.

If one wants to have the -b and -n options in a PyROOT script, this can be achieved with a simple callback function for the OptionParser and importing ROOT after dealing with the other command line options. Like in this example script:

#!/usr/bin/env python
import sys
from optparse import OptionParser

grootargs = []
def callback_rootargs(option, opt, value, parser):
    grootargs.append(opt)

parser = OptionParser('drawemptycanvas.py [-b] [-n]')
parser.add_option("-b",action="callback",callback=callback_rootargs,
                  help="run in batch mode wihtout graphics.")
parser.add_option("-n",action="callback",callback=callback_rootargs,
                  help="do not execute logon and logoff macros as"
                  "specified in .rootrc")

(options, args) = parser.parse_args()

sys.argv = grootargs
from ROOT import TCanvas

TCanvas()

Indeed this is not pretty, but works very good.

Cheers
Matthias