Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Allow command line options to called scripts #37

Closed
gak opened this issue Aug 12, 2009 · 5 comments
Closed

Allow command line options to called scripts #37

gak opened this issue Aug 12, 2009 · 5 comments

Comments

@gak
Copy link
Owner

gak commented Aug 12, 2009

I was amazed at how simple it was to install and use pycallgraph. Just one suggestion.

It would be very helpful if you could specify sys.argv to the called script on the command line. That way I wouldn't have to make a new script file for every set of options. If you put the output filename before the scriptfile you could feed the rest of the command line to the called script. For example:

{{{
pycallgraph-dot.py test1.png myscript.py --spam=42 ham.txt eggs.txt
}}}

@gak
Copy link
Owner Author

gak commented Aug 12, 2009

Author: anonymous
Hi,

you can achieve this by the following:

change:

execfile(args[0])

to:

saveargs = sys.argv # save sys.argv
sys.argv = [args[0]] + args[2:] # store custom sys.argv
execfile(args[0])
sys.argv = saveargs # restore sys.argv in case it is needed later...

in scripts/pycallgraph-dot.py (version 0.4.1).

Then you can supply arguments to the script after stopping argument processing with '--'.

Example:
pycallgraph-dot.py which pycallgraph-dot.py /tmp/test.png -- -h

The -h option is then passed to the exec'd version of pycallgraph-dot.py.

Best,
Staal

@gak
Copy link
Owner Author

gak commented Aug 12, 2009

Author: anonymous
Sorry about the formatting in the above comment. Forgot to preview...
So, the hack (it is a hack) should have looked like this:

Change lines 109-110 in pycallgraph-dot.py (version 0.4.1):

{{{
execfile(args[0])
pycallgraph.make_dot_graph(args[1], options.format, options.tool)
}}}

to

{{{
_my_saveargs = sys.argv
sys.argv = [args[0]] + args[2:]
_my_imagefile = args[1]
_my_options_format = options.format
_my_options_tool = options.tool
execfile(args[0])
sys.argv = _my_saveargs
pycallgraph.make_dot_graph(_my_imagefile, _my_options_format, _my_options_tool)
}}}

For this to work, we hope that the code in args[0] does not alter any globals that are needed by pycallgraph, in particular the globals that start with 'my'.

Best,
Staal

@gak
Copy link
Owner Author

gak commented Aug 12, 2009

Author: gak

2 similar comments
@gak
Copy link
Owner Author

gak commented Aug 12, 2009

Author: gak

@gak
Copy link
Owner Author

gak commented Aug 12, 2009

Author: gak

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant