Mayavi2 standalone script with command line arguments
Question: I am trying to parse command line argument to a MayaVi2 standalone script.
However, the `mayavi2.standalone()` function eats command line arguments
before me. For example:

    
    
    #! /usr/bin/python
    import sys, argparse
    from mayavi.scripts import mayavi2
    from mayavi import mlab
    
    @mayavi2.standalone
    def view():
        mayavi.new_scene()
        mlab.test_plot3d()
    
    
    def parseCmdLineArgs():
        parser = argparse.ArgumentParser(description='Simple plotting using MayaVi2')
        parser.add_argument('--scale', dest='scale', action='store',help='Sets the axis scaling')
        parser.set_defaults(scale=1.0)
        args = parser.parse_args(sys.argv[1:])
        return args
    
    if __name__ == '__main__':
        args=parseCmdLineArgs()
        print "Scale=%g" % args.scale
        view()
    

If call this script `plot.py` and run it as

    
    
    $ plot.py -h
    

I get the `mayavi2.standalone()` help message and not the one for my own
parser.


Answer: From the source code of the `mayavi2` module at
[GitHub](https://github.com/enthought/mayavi/blob/master/mayavi/scripts/mayavi2.py),
it is observed that it has code in the global name space. Some of the code
will execute command line parsing. Since Python code is run from top to down,
the problem can be solved by a reordering of the code:

    
    
    import sys, argparse
    
    def parseCmdLineArgs():
        parser = argparse.ArgumentParser(description='Simple plotting using MayaVi2')
        parser.add_argument('--scale', dest='scale', action='store',help='Sets the axis scaling')
        parser.set_defaults(scale=1.0)
        args = parser.parse_args(sys.argv[1:])
        return args
    
    if __name__ == '__main__':
        args=parseCmdLineArgs()
    
    from mayavi.scripts import mayavi2
    from mayavi import mlab
    
    @mayavi2.standalone
    def view():
        mayavi.new_scene()
        mlab.test_plot3d()
    
    
    if __name__ == '__main__':
        print "Scale=%g" % args.scale
        view()
    

