command line arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TMS
    New Member
    • Sep 2006
    • 119

    #1

    command line arguments

    Let's say I want to call one of my modules from the command line, for instance I have this find_root(f,xLo , xHi, eps) assignment I've been working on.

    I have the header that looks like this:

    Code:
    import sys
    
    sys.argv[1,2,3,4]
    Wouldn't that be enough that when I go to a terminal screen (Ubuntu) or console (windows) I could simply type:

    find_root.py find_root('x**2 - 2, 0, 3, .0001) and it would work? Assuming that my arguments and the function itself is correct. I'm only looking at the command line arguments right now.

    Thank you
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by TMS
    Let's say I want to call one of my modules from the command line, for instance I have this find_root(f,xLo , xHi, eps) assignment I've been working on.

    I have the header that looks like this:

    Code:
    import sys
    
    sys.argv[1,2,3,4]
    Wouldn't that be enough that when I go to a terminal screen (Ubuntu) or console (windows) I could simply type:

    find_root.py find_root('x**2 - 2, 0, 3, .0001) and it would work? Assuming that my arguments and the function itself is correct. I'm only looking at the command line arguments right now.

    Thank you
    This works at the windows command prompt:

    C:\>python find_root.py x**2-4 3 0 0.0001

    Code:
    ...function definition....
    
    if __name__ == '__main__':
        import sys
        find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
    At the python command line:
    Code:
    from find_root import find_root
    find_root('x**2-4', 3, 0, 0.0001)
    Is there another way?

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bvdet
      This works at the windows command prompt:

      C:\>python find_root.py x**2-4 3 0 0.0001

      Code:
      ...function definition....
      
      if __name__ == '__main__':
          import sys
          find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
      At the python command line:
      Code:
      from find_root import find_root
      find_root('x**2-4', 3, 0, 0.0001)
      Is there another way?
      Code:
      ...function definition....
      
      if __name__ == '__main__':
          import sys
          find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
      is not quite complete because errors will be thrown when too few arguments a sent from the command line or if those arguments can't be converted to float. Most command line scripts will provide help when this happens. Elaborate on this idea:

      Code:
      ...function definition....
      
      if __name__ == '__main__':
          import sys
          try:
              find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
          except (IndexError, TypeError):
              print "Usage: Too few arguments"
      It's a little nicer than showing a python traceback.

      Comment

      • TMS
        New Member
        • Sep 2006
        • 119

        #4
        Thank you! Good information.

        TMS

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          I have a suggestion for a slight improvement. If you find that you are entering in the same value for 'eps' consistently, add a default to the function argument list:
          Code:
          def find_root(f, xLo, xHi, eps=0.00001):
          ..................
          ..................
          Then the function call could be:
          Code:
          find_root('5*x**3+2*x**2-x+1', -3, 4)
          # if you need a value for eps different from the default
          find_root('5*x**3+2*x**2-x+1', -3, 4, .001)

          Comment

          Working...