CVS: tmda/bin tmda-address,1.11,1.12

"Jason R. Mastaler" <[email protected]>
Newsgroups gmane.mail.spam.tmda.cvs
Message-ID <[email protected]>
Update of /cvsroot/tmda/tmda/bin
In directory sc8-pr-cvs1:/tmp/cvs-serv22982/bin

Modified Files:
	tmda-address 
Log Message:
Bugfix.  There was order dependance in the option processing of
tmda-address. e.g,:

$ tmda-address -d 1d -a [email protected]
[email protected]
  
$ tmda-address -a [email protected] -d 1d
[email protected]
  
The same behavior also applies if you use -c and a config file that
changes the relevant config options.  

The problem is caused by the behavior of Python's getopt, which stops
processing options when it reaches a non-option argument (unlike GNU
getopt).  Since -d doesn't require an argument, the "1d" in the first
example below is a non-option argument, which stops the option
processing, leaving '1d', '-a', and '[email protected]' in args.  Since
the -k and -s options require an argument, they don't exhibit the same
behavior.

Change summary:
- move getopt invocation to a subroutine
- do initial getopt using new subroutine with sys.argv[1:]
- add an outer loop to redo the opts processing while there are items
  left in the opts list
- change opts processing to use a copy of opts, so that opts list can
  be changed in the loop
- add code to remove opts from the list after they're processed
- add code to look for more opts in args list after -d is handled
  - remove args that don't start with a '-'
  - send remaining args to new subroutine, appending any detected
    options to opts list

Thanks to Ed Blackman for the fix.


Index: tmda-address
===================================================================
RCS file: /cvsroot/tmda/tmda/bin/tmda-address,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- tmda-address	27 Apr 2003 07:58:18 -0000	1.11
+++ tmda-address	15 May 2003 21:11:35 -0000	1.12
@@ -83,8 +83,10 @@
         print msg
     sys.exit(code)
 
-try:
-    opts, args = getopt.getopt(sys.argv[1:],
+def processOpts(args):
+    opts = None
+    try:
+	opts, args = getopt.getopt(args,
                                    'c:a:dk:s:hVn', ['config-file=',
                                                     'address=',
                                                     'dated',
@@ -93,45 +95,57 @@
                                                     'help',
                                                     'version',
                                                     'no-newline'])
-except getopt.error, msg:
-    usage(1, msg)
+    except getopt.error, msg:
+	usage(1, msg)
+    return (opts, args)
 
+opts, args = processOpts(sys.argv[1:])
 address = None
 tag = 'dated'
 option = None
 print_newline = 1
 
-for opt, arg in opts:
+for opt, arg in opts[:]:
     if opt in ('-c', '--config-file'):
         os.environ['TMDARC'] = arg
+	opts.remove((opt, arg))
 
 from TMDA import Defaults
 
-for opt, arg in opts:
-    if opt in ('-h', '--help'):
-        usage(0)
-    if opt == '-V':
-        print Version.ALL
-        sys.exit()
-    if opt == '--version':
-        print Version.TMDA
-        sys.exit()
-    elif opt in ('-a', '--address'):
-        address = arg
-    elif opt in ('-d', '--dated'):
-        tag = Defaults.TAGS_DATED[0].lower()
-        try:                            # check for timeout override
-            os.environ['TMDA_TIMEOUT'] = args[0]
-        except IndexError:
-            pass
-    elif opt in ('-k', '--keyword'):
-        tag = Defaults.TAGS_KEYWORD[0].lower()
-        option = arg
-    elif opt in ('-s', '--sender'):
-        tag = Defaults.TAGS_SENDER[0].lower()
-        option = arg
-    elif opt in ('-n', '--no-newline'):
-        print_newline = 0
+while len(opts) > 0:
+    for opt, arg in opts[:]:
+	if opt in ('-h', '--help'):
+	    usage(0)
+	if opt == '-V':
+	    print Version.ALL
+	    sys.exit()
+	if opt == '--version':
+	    print Version.TMDA
+	    sys.exit()
+	elif opt in ('-a', '--address'):
+	    address = arg
+	elif opt in ('-d', '--dated'):
+	    tag = Defaults.TAGS_DATED[0].lower()
+	    option = None
+	    try:                        # check for timeout override
+		os.environ['TMDA_TIMEOUT'] = args[0]
+	    except IndexError:
+		pass
+	    for tmparg in args[:]:
+                if tmparg[0] == '-':
+		    break
+		args.remove(tmparg)
+	    moreOpts, args = processOpts(args)
+	    opts.extend(moreOpts)
+	elif opt in ('-k', '--keyword'):
+	    tag = Defaults.TAGS_KEYWORD[0].lower()
+	    option = arg
+	elif opt in ('-s', '--sender'):
+	    tag = Defaults.TAGS_SENDER[0].lower()
+	    option = arg
+	elif opt in ('-n', '--no-newline'):
+	    print_newline = 0
+	opts.remove((opt, arg))
 
 
 from TMDA import Cookie

_______________________________________
tmda-cvs mailing list
http://tmda.net/lists/listinfo/tmda-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.