Thanks,
Jeff Elkins
try:
arg1 = sys.argv[1]
except IndexError:
print "This script takes an argument, you boob!"
sys.exit(1)
OR, way better: See the optparse module.
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
Thanks!
Also possible, to guarantee that exactly one argument was given:
try:
arg1, = sys.argv
except ValueError:
print "This script takes an argument, you boob!"
sys.exit(1)
If you want to get, say, 3 arguments, just change that line to:
arg1, arg2, arg3 = sys.argv
> OR, way better: See the optparse module.
Definitely. Though depending on what kind of arguments your script
takes, you still may need to deal with the args that optparse returns.
STeVe
look at sys.argv
Regards,
Philippe
Surely this would give an unpacking error if command-line arguments
*were* present.
> If you want to get, say, 3 arguments, just change that line to:
>
> arg1, arg2, arg3 = sys.argv
>
Similarly. Or are you just calling sys.argv[0] arg1 to confuse me? ;-)
>
>>OR, way better: See the optparse module.
>
>
> Definitely. Though depending on what kind of arguments your script
> takes, you still may need to deal with the args that optparse returns.
>
Anyway it's always good to know about the underlying mechanisms.
regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
> script1.py
> -=-=-=-=-=-=-=-=-
> import sys
>
> for x in range(len(sys.argv)):
> print "Argument %s is %s" % (x, sys.argv[x])
> -=-=-=-=-=-=-=-=-
>
> (watch out for line wrapping)
>
> E:\UserData\Dennis Lee Bieber\My Documents>script1.py "These are the"
> Voyages
> Argument 0 is E:\UserData\Dennis Lee Bieber\My Documents\Script1.py
> Argument 1 is These are the
> Argument 2 is Voyages
>
> E:\UserData\Dennis Lee Bieber\My Documents>python script1.py "quoted
> arg" regular args
> Argument 0 is script1.py
> Argument 1 is quoted arg
> Argument 2 is regular
> Argument 3 is args
>
> E:\UserData\Dennis Lee Bieber\My Documents>
>
>
So?
Oops. Yup. Change all "sys.argv" to "sys.argv[1:]". Sorry, I never
use sys.argv directly anymore; I always get my argv through optparse,
which kindly strips off sys.argv[0]. Good catch, thanks!
STeVe