Iterating command switches from a data file - have a working solution but it seems inefficient

Eric Deveaud edeveaud at pasteur.fr
Thu Apr 13 04:32:29 EDT 2006


News wrote:
>  Hi everyone,
> 
>  My goal is to pull command switches/options from a file and then assign
>  the values to select variables which would eventually be included in a
>  class object.
> 
>  The data file looks something like this but the switches could be in any
>  order and not all may be used.
> 
>  -m quemanager -s server -p port -k key -o object -c 20 -t test at email.com
> 
>  Also, please keep in mind that the source code will have more than one
>  line in it and each has to be treaded separately.

I suggest you use getopt or optpase to perform this task
those modules are designed to perform what you want, with error checking

import getopt
inp = open("const.txt","r")
for line in inp:
    try:
       opt, args = getopt.getopt(iline.split(), 'c:k:m:o:p:s:t:i')
       print opt
   except getopt.GetoptError, msg:
       # handle the error as you need

		Eric
-- 
 Je voudrais savoir s'il existe un compteur de vitesse (?) pour savoir
 à quelle vitesse on est réellement connecté au FAI et surtout si une
 telle bête existe... où la trouver. 
 -+- RJ in: Guide du Neuneu d'Usenet - Baisse la tête et pédale -+-



More information about the Python-list mailing list