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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TP

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

    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)
  • Arnaud Delobelle

    #2
    Re: script that parses command line, and execfile('')

    TP <Tribulations@P aralleles.inval idwrites:
    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('scr ipt.py')
    But I have to say I have never felt the need to use execfile() this way.

    --
    Arnaud

    Comment

    • Diez B. Roggisch

      #3
      Re: script that parses command line, and execfile('')

      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(arg uments)


      instead.

      Diez

      Comment

      • Sandip Bhattacharya

        #4
        Re: script that parses command line, and execfile('')

        On Nov 4, 12:43 am, "Diez B. Roggisch" <de...@nospam.w eb.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

        Comment

        • Tim Chase

          #5
          Re: script that parses command line, and execfile('')

          Sandip Bhattacharya wrote:
          On Nov 4, 12:43 am, "Diez B. Roggisch" <de...@nospam.w eb.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]


          [2]

          (at the "prog" entry at the bottom)

          Comment

          Working...