Code:
#!/usr/bin/env python # file: echo.py import sys print sys.argv
Output:
> chmod +x echo.py > echo.py tuna ['echo.py', 'tuna'] > echo.py tuna fish ['echo.py', 'tuna', 'fish'] > echo.py "tuna fish" ['echo.py', 'tuna fish'] > echo.py ['echo.py'] >
Code:
#!/usr/bin/env python
# file: hypotenuse.py
import sys, math
if len(sys.argv) != 3: # the program name and the two arguments
# stop the program and print an error message
sys.exit("Must provide two positive numbers")
# Convert the two arguments from strings into numbers
x = float(sys.argv[1])
y = float(sys.argv[2])
print "Hypotenuse =", math.sqrt(x**2+y**2)
Output:
> hypotenuse.py 5 12 Hypotenuse = 13.0 >
math.sqrt("16")
math.sqrt("16a")
math.sqrt("16a25")
math.sqrt("sixteen")
"1" + 2
?
This page based on Processing Command Line Arguments.