##// END OF EJS Templates
py3: make a bytes version of getopt.getopt()...
Pulkit Goyal -
r30578:c6ce11f2 default
parent child Browse files
Show More
@@ -7,10 +7,11 b''
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import getopt
11
12 from .i18n import _
10 from .i18n import _
13 from . import error
11 from . import (
12 error,
13 pycompat,
14 )
14
15
15 # Set of flags to not apply boolean negation logic on
16 # Set of flags to not apply boolean negation logic on
16 nevernegate = set([
17 nevernegate = set([
@@ -34,13 +35,14 b' def gnugetopt(args, options, longoptions'
34 stopindex = args.index('--')
35 stopindex = args.index('--')
35 extraargs = args[stopindex + 1:]
36 extraargs = args[stopindex + 1:]
36 args = args[:stopindex]
37 args = args[:stopindex]
37 opts, parseargs = getopt.getopt(args, options, longoptions)
38 opts, parseargs = pycompat.getoptb(args, options, longoptions)
38 args = []
39 args = []
39 while parseargs:
40 while parseargs:
40 arg = parseargs.pop(0)
41 arg = parseargs.pop(0)
41 if arg and arg[0] == '-' and len(arg) > 1:
42 if arg and arg[0] == '-' and len(arg) > 1:
42 parseargs.insert(0, arg)
43 parseargs.insert(0, arg)
43 topts, newparseargs = getopt.getopt(parseargs, options, longoptions)
44 topts, newparseargs = pycompat.getoptb(parseargs,\
45 options, longoptions)
44 opts = opts + topts
46 opts = opts + topts
45 parseargs = newparseargs
47 parseargs = newparseargs
46 else:
48 else:
@@ -125,7 +127,7 b' def fancyopts(args, options, state, gnu='
125 if gnu:
127 if gnu:
126 parse = gnugetopt
128 parse = gnugetopt
127 else:
129 else:
128 parse = getopt.getopt
130 parse = pycompat.getoptb
129 opts, args = parse(args, shortlist, namelist)
131 opts, args = parse(args, shortlist, namelist)
130
132
131 # transfer result to state
133 # transfer result to state
@@ -10,6 +10,7 b' This contains aliases to hide python ver'
10
10
11 from __future__ import absolute_import
11 from __future__ import absolute_import
12
12
13 import getopt
13 import os
14 import os
14 import sys
15 import sys
15
16
@@ -87,6 +88,19 b' if ispy3:'
87 setattr = _wrapattrfunc(builtins.setattr)
88 setattr = _wrapattrfunc(builtins.setattr)
88 xrange = builtins.range
89 xrange = builtins.range
89
90
91 # getopt.getopt() on Python 3 deals with unicodes internally so we cannot
92 # pass bytes there. Passing unicodes will result in unicodes as return
93 # values which we need to convert again to bytes.
94 def getoptb(args, shortlist, namelist):
95 args = [a.decode('latin-1') for a in args]
96 shortlist = shortlist.decode('latin-1')
97 namelist = [a.decode('latin-1') for a in namelist]
98 opts, args = getopt.getopt(args, shortlist, namelist)
99 opts = [(a[0].encode('latin-1'), a[1].encode('latin-1'))
100 for a in opts]
101 args = [a.encode('latin-1') for a in args]
102 return opts, args
103
90 else:
104 else:
91 def sysstr(s):
105 def sysstr(s):
92 return s
106 return s
@@ -106,6 +120,9 b' else:'
106 def fsdecode(filename):
120 def fsdecode(filename):
107 return filename
121 return filename
108
122
123 def getoptb(args, shortlist, namelist):
124 return getopt.getopt(args, shortlist, namelist)
125
109 osname = os.name
126 osname = os.name
110 ospathsep = os.pathsep
127 ospathsep = os.pathsep
111 ossep = os.sep
128 ossep = os.sep
@@ -116,6 +116,10 b' import tempfile'
116 import threading
116 import threading
117 import time
117 import time
118
118
119 from . import (
120 pycompat,
121 )
122
119 defaultdict = collections.defaultdict
123 defaultdict = collections.defaultdict
120 contextmanager = contextlib.contextmanager
124 contextmanager = contextlib.contextmanager
121
125
@@ -771,7 +775,7 b' def main(argv=None):'
771
775
772 # process options
776 # process options
773 try:
777 try:
774 opts, args = getopt.getopt(sys.argv[optstart:], "hl:f:o:p:",
778 opts, args = pycompat.getoptb(sys.argv[optstart:], "hl:f:o:p:",
775 ["help", "limit=", "file=", "output-file=", "script-path="])
779 ["help", "limit=", "file=", "output-file=", "script-path="])
776 except getopt.error as msg:
780 except getopt.error as msg:
777 print(msg)
781 print(msg)
General Comments 0
You need to be logged in to leave comments. Login now