By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.