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

T

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?

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)
 
A

Arnaud Delobelle

TP said:
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?


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.
 
D

Diez B. Roggisch

TP said:
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?


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
 
S

Sandip Bhattacharya

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
 
T

Tim Chase

Sandip said:
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/optparse-parsing-arguments.html

[2]
http://www.python.org/doc/2.5.2/lib/optparse-creating-parser.html
(at the "prog" entry at the bottom)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top