passing arguments
Steve Holden
steve at holdenweb.com
Sun May 22 16:12:06 EDT 2005
Steven Bethard wrote:
> James Stroud wrote:
>
>>import sys
>>
>>try:
>> arg1 = sys.argv[1]
>>except IndexError:
>> print "This script takes an argument, you boob!"
>> sys.exit(1)
>
>
> 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)
>
Aren't we forgetting argv[0] here, or am I overlooking something (like,
you chopped it off without telling me?)?
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/
More information about the Python-list
mailing list