##// 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 8 from __future__ import absolute_import
9 9
10 import getopt
11
12 10 from .i18n import _
13 from . import error
11 from . import (
12 error,
13 pycompat,
14 )
14 15
15 16 # Set of flags to not apply boolean negation logic on
16 17 nevernegate = set([
@@ -34,13 +35,14 b' def gnugetopt(args, options, longoptions'
34 35 stopindex = args.index('--')
35 36 extraargs = args[stopindex + 1:]
36 37 args = args[:stopindex]
37 opts, parseargs = getopt.getopt(args, options, longoptions)
38 opts, parseargs = pycompat.getoptb(args, options, longoptions)
38 39 args = []
39 40 while parseargs:
40 41 arg = parseargs.pop(0)
41 42 if arg and arg[0] == '-' and len(arg) > 1:
42 43 parseargs.insert(0, arg)
43 topts, newparseargs = getopt.getopt(parseargs, options, longoptions)
44 topts, newparseargs = pycompat.getoptb(parseargs,\
45 options, longoptions)
44 46 opts = opts + topts
45 47 parseargs = newparseargs
46 48 else:
@@ -125,7 +127,7 b' def fancyopts(args, options, state, gnu='
125 127 if gnu:
126 128 parse = gnugetopt
127 129 else:
128 parse = getopt.getopt
130 parse = pycompat.getoptb
129 131 opts, args = parse(args, shortlist, namelist)
130 132
131 133 # transfer result to state
@@ -10,6 +10,7 b' This contains aliases to hide python ver'
10 10
11 11 from __future__ import absolute_import
12 12
13 import getopt
13 14 import os
14 15 import sys
15 16
@@ -87,6 +88,19 b' if ispy3:'
87 88 setattr = _wrapattrfunc(builtins.setattr)
88 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 104 else:
91 105 def sysstr(s):
92 106 return s
@@ -106,6 +120,9 b' else:'
106 120 def fsdecode(filename):
107 121 return filename
108 122
123 def getoptb(args, shortlist, namelist):
124 return getopt.getopt(args, shortlist, namelist)
125
109 126 osname = os.name
110 127 ospathsep = os.pathsep
111 128 ossep = os.sep
@@ -116,6 +116,10 b' import tempfile'
116 116 import threading
117 117 import time
118 118
119 from . import (
120 pycompat,
121 )
122
119 123 defaultdict = collections.defaultdict
120 124 contextmanager = contextlib.contextmanager
121 125
@@ -771,7 +775,7 b' def main(argv=None):'
771 775
772 776 # process options
773 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 779 ["help", "limit=", "file=", "output-file=", "script-path="])
776 780 except getopt.error as msg:
777 781 print(msg)
General Comments 0
You need to be logged in to leave comments. Login now