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

command line arguments

TMS
100+
P: 119
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:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. sys.argv[1,2,3,4]
  4.  
  5.  
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
Jan 24 '07 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. sys.argv[1,2,3,4]
  4.  
  5.  
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

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
At the python command line:
Expand|Select|Wrap|Line Numbers
  1. from find_root import find_root
  2. find_root('x**2-4', 3, 0, 0.0001)
Is there another way?
Jan 24 '07 #2

bartonc
Expert 5K+
P: 6,596
This works at the windows command prompt:

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

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
At the python command line:
Expand|Select|Wrap|Line Numbers
  1. from find_root import find_root
  2. find_root('x**2-4', 3, 0, 0.0001)
Is there another way?
Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
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:

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     try:
  6.         find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  7.     except (IndexError, TypeError):
  8.         print "Usage: Too few arguments"
  9.  
It's a little nicer than showing a python traceback.
Jan 25 '07 #3

TMS
100+
P: 119
TMS
Thank you! Good information.

TMS
Jan 25 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. def find_root(f, xLo, xHi, eps=0.00001):
  2. ..................
  3. ..................
Then the function call could be:
Expand|Select|Wrap|Line Numbers
  1. find_root('5*x**3+2*x**2-x+1', -3, 4)
  2. # if you need a value for eps different from the default
  3. find_root('5*x**3+2*x**2-x+1', -3, 4, .001)
Jan 25 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.