def gnu_getopt(args, shortopts, longopts=[]):
 opts = []
 prog_args = []
 if isinstance(longopts, str):
  longopts = [longopts]
 else:
  longopts = list(longopts)
 if shortopts.startswith('+'):
  shortopts = shortopts[1:]
  all_options_first = True
 elif os.environ.get('POSIXLY_CORRECT'):
  all_options_first = True
 else:
  all_options_first = False
 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:])
  elif all_options_first:
   prog_args += args
   break
  else:
   prog_args.append(args[0])
   args = args[1:]
 return (opts, prog_args)

def gnu_getopt ( args , shortopts , longopts = [ ] ) : opts = [ ] prog_args = [ ] if isinstance ( longopts , str ) : longopts = [ longopts ] else : longopts = list ( longopts ) if shortopts . startswith ( '+' ) : shortopts = shortopts [ 1 : ] all_options_first = True elif os . environ . get ( 'POSIXLY_CORRECT' ) : all_options_first = True else : all_options_first = False 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 : ] ) elif all_options_first : prog_args += args break else : prog_args . append ( args [ 0 ] ) args = args [ 1 : ] return ( opts , prog_args )
getopt -> opts .