[Python-checkins] python/dist/src/Lib getopt.py,1.17,1.18

loewis@users.sourceforge.net loewis@users.sourceforge.net
Thu, 06 Jun 2002 03:58:38 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv9636/Lib

Modified Files:
	getopt.py 
Log Message:
Patch 473512: add GNU style scanning as gnu_getopt.


Index: getopt.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/getopt.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** getopt.py	4 Apr 2002 22:55:58 -0000	1.17
--- getopt.py	6 Jun 2002 10:58:35 -0000	1.18
***************
*** 6,12 ****
  and `--').  Long options similar to those supported by GNU software
  may be used as well via an optional third argument.  This module
! provides a single function and an exception:
  
  getopt() -- Parse command line options
  GetoptError -- exception (class) raised with 'opt' attribute, which is the
  option involved with the exception.
--- 6,14 ----
  and `--').  Long options similar to those supported by GNU software
  may be used as well via an optional third argument.  This module
! provides two functions and an exception:
  
  getopt() -- Parse command line options
+ gnu_getopt() -- Like getopt(), but allow option and non-option arguments
+ to be intermixed. 
  GetoptError -- exception (class) raised with 'opt' attribute, which is the
  option involved with the exception.
***************
*** 14,23 ****
  
  # Long option support added by Lars Wirzenius <liw@iki.fi>.
! 
  # Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions
  # to class-based exceptions.
  
  __all__ = ["GetoptError","error","getopt"]
  
  class GetoptError(Exception):
      opt = ''
--- 16,39 ----
  
  # Long option support added by Lars Wirzenius <liw@iki.fi>.
! #
  # Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions
  # to class-based exceptions.
+ #
+ # Peter Åstrand <astrand@lysator.liu.se> added gnu_getopt().
+ #
+ # TODO for gnu_getopt():
+ #
+ # - GNU getopt_long_only mechanism
+ # - allow the caller to specify ordering
+ # - RETURN_IN_ORDER option
+ # - GNU extension with '-' as first character of option string
+ # - optional arguments, specified by double colons
+ # - a option string with a W followed by semicolon should
+ #   treat "-W foo" as "--foo"
  
  __all__ = ["GetoptError","error","getopt"]
  
+ import os
+ 
  class GetoptError(Exception):
      opt = ''
***************
*** 75,78 ****
--- 91,144 ----
  
      return opts, args
+ 
+ def gnu_getopt(args, shortopts, longopts = []):
+     """getopt(args, options[, long_options]) -> opts, args
+ 
+     This function works like getopt(), except that GNU style scanning
+     mode is used by default. This means that option and non-option
+     arguments may be intermixed. The getopt() function stops
+     processing options as soon as a non-option argument is
+     encountered.
+ 
+     If the first character of the option string is `+', or if the
+     environment variable POSIXLY_CORRECT is set, then option
+     processing stops as soon as a non-option argument is encountered.
+     
+     """
+ 
+     opts = []
+     prog_args = []
+     if type(longopts) == type(""):
+         longopts = [longopts]
+     else:
+         longopts = list(longopts)
+ 
+     # Allow options after non-option arguments?
+     if shortopts.startswith('+'):
+         shortopts = shortopts[1:]
+         all_options_first = 1
+     elif os.getenv("POSIXLY_CORRECT"):
+         all_options_first = 1
+     else:
+         all_options_first = 0
+ 
+     while args:
+         if args[0] == '--':
+             prog_args += args[1:]
+             break
+ 
+         if args[0][:2] == '--':
+             opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
+         elif args[0][:1] == '-':
+             opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
+         else:
+             if all_options_first:
+                 prog_args += args
+                 break
+             else:
+                 prog_args.append(args[0])
+                 args = args[1:]
+ 
+     return opts, prog_args
  
  def do_longs(opts, opt, longopts, args):