getopt index out of range

Matthew Sacks ntwrkd at gmail.com
Wed Feb 11 19:58:56 CET 2009


>because the traceback says the index is 0 and there's only one line with a 0 in it!
Indeed. Thank you.


On Wed, Feb 11, 2009 at 7:11 AM, MRAB <google at mrabarnett.plus.com> wrote:
> Steve Holden wrote:
>>
>> Matthew Sacks wrote:
>>>
>>> Hi List,
>>> I am getting an index out of range error when trying to parse with
>>> getopt.
>>> Probably something simple. Any suggestions are appreciated
>>>
>>> optlist, args = getopt.getopt(sys.argv[1:], 'h', ['connectPassword=',
>>> 'adminServerURL=', 'action=', 'targets=', 'appDir='])
>>>
>>>
>>> #Assign Opts
>>> connectPassword = optlist[0][1]
>>> adminServerURL = optlist[1][1]
>>> action = optlist[2][1]
>>> targets = optlist[3][1]
>>> appDir = optlist[4][1]
>>>
>>> #this statement never gets executed
>>> print "Args: " + connectPassword + " " + adminServerURL + " " + action
>>> + " " + targets + " " + appDir
>>>
>>>  File "/home/msacks/untitled4.py", line 23, in ?
>>> IndexError: index out of range: 0
>>>
>> It might help a little if you made it more obvious which was line 23 ...
>>
> It a guess I'd say it was:
>
>    connectPassword = optlist[0][1]
>
> because the traceback says the index is 0 and there's only one line with a 0
> in it!
>
>> The real problem, though, is a misunderstanding of what getopt.getopt()
>> returns. The (option, value) pairs are only returned for options found
>> in the command line, so you can't guarantee there'll be four.
>>
>> Set your values to defaults, then adjust for those for which an option
>> is found in the list, as in the "typical usage" example in the Fine
>> Manual.
>>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list