getopt index out of range

Steve Holden steve at holdenweb.com
Wed Feb 11 09:26:38 EST 2009


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 ...

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.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list