467,882 Members | 1,237 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,882 developers. It's quick & easy.

script that parses command line, and execfile('')

TP
Hello,

I have a script that uses the "optparse" package to parse the command line.
For example:

$ script.py --help
# displays help about script.py

Is this possible to call such a script with execfile('') once in the Python
interactive shell?
>>execfile( 'script.py' )
I get errors because there is no argv dictionary when used with execfile.

How to solve this problem, so that I am able to use script.py in command
line as well as with execfile?

Thanks

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
Nov 3 '08 #1
  • viewed: 6756
Share:
4 Replies
TP <Tr**********@Paralleles.invalidwrites:
Hello,

I have a script that uses the "optparse" package to parse the command line.
For example:

$ script.py --help
# displays help about script.py

Is this possible to call such a script with execfile('') once in the Python
interactive shell?
>>>execfile( 'script.py' )

I get errors because there is no argv dictionary when used with execfile.

How to solve this problem, so that I am able to use script.py in command
line as well as with execfile?
Have you tried setting sys.argv manually?

e.g.
>>import sys
sys.argv = ['--help']
execfile('script.py')
But I have to say I have never felt the need to use execfile() this way.

--
Arnaud
Nov 3 '08 #2
TP schrieb:
Hello,

I have a script that uses the "optparse" package to parse the command line.
For example:

$ script.py --help
# displays help about script.py

Is this possible to call such a script with execfile('') once in the Python
interactive shell?
>>>execfile( 'script.py' )

I get errors because there is no argv dictionary when used with execfile.

How to solve this problem, so that I am able to use script.py in command
line as well as with execfile?

Don't use execfile. Make script.py like this:
....

def main(argv=None):
if argv is None: argv = sys.argv[1:]
...
Then just do

import script
script.main(arguments)
instead.

Diez
Nov 3 '08 #3
On Nov 4, 12:43*am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
def main(argv=None):
* * *if argv is None: argv = sys.argv[1:]
* * *...
Wouldn't that make optparse miss the first parameter sys.argv[1]
mistaking it to be the name of the current program?

- Sandip
Nov 4 '08 #4
Sandip Bhattacharya wrote:
On Nov 4, 12:43 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>def main(argv=None):
if argv is None: argv = sys.argv[1:]
...

Wouldn't that make optparse miss the first parameter sys.argv[1]
mistaking it to be the name of the current program?
Nope...optparse uses argv[1:] as documented at [1]. The "prog"
argument can be specified in the constructor to OptionParser, but
defaults to sys.argv[0] if it's not been explicitly specified.[2]

-tkc
[1]
http://www.python.org/doc/2.5.2/lib/...arguments.html

[2]
http://www.python.org/doc/2.5.2/lib/...ng-parser.html
(at the "prog" entry at the bottom)
Nov 4 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Sticks | last post: by
2 posts views Thread by Jonathan | last post: by
4 posts views Thread by Ian Giblin | last post: by
1 post views Thread by Brian Blais | last post: by
5 posts views Thread by Helmut Jarausch | last post: by
6 posts views Thread by Alan Isaac | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.