FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use optparse for Python 2.6 compatibility. · feenes/unidecode@701e770 · GitHub

Repository navigation

Commit 701e770

Browse files
committed
Use optparse for Python 2.6 compatibility.
1 parent 2d41919 commit 701e770

1 file changed

Lines changed: 15 additions & 21 deletions

File tree

‎unidecode/util.py‎

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vim:ts=4 sw=4 expandtab softtabstop=4
22
from __future__ import print_function
3-
import argparse
3+
import optparse
44
import locale
55
import os
66
import sys
@@ -11,25 +11,20 @@
1111
PY3 = sys.version_info[0] >= 3
1212

1313
def main():
14-
parser = argparse.ArgumentParser('unidecode')
15-
parser.add_argument('-e',
16-
'--encoding',
17-
nargs=1,
18-
metavar='ENCODING',
19-
help='Specify an encoding, overriding system default')
20-
parser.add_argument('file',
21-
nargs='?',
22-
metavar='FILE',
23-
help='File to transliterate')
24-
25-
args = parser.parse_args()
26-
if args.encoding:
27-
encoding = args.encoding[0]
28-
else:
29-
encoding = locale.getpreferredencoding()
14+
default_encoding = locale.getpreferredencoding()
15+
16+
parser = optparse.OptionParser('%prog [options] [FILE]',
17+
description="Transliterate Unicode text into ASCII. FILE is path to file to transliterate. "
18+
"If omitted, standard input is used.")
19+
parser.add_option('-e', '--encoding', metavar='ENCODING', default=default_encoding,
20+
help='Specify an encoding (default is %s)' % (default_encoding,))
21+
22+
options, args = parser.parse_args()
23+
24+
encoding = options.encoding
3025

31-
if args.file:
32-
with open(args.file, 'rb') as f:
26+
if args:
27+
with open(args[0], 'rb') as f:
3328
stream = f.read()
3429
else:
3530
if PY3:
@@ -40,8 +35,7 @@ def main():
4035
try:
4136
stream = stream.decode(encoding)
4237
except UnicodeDecodeError as e:
43-
msg = ('Unable to decode input: {}, start: {:d}, '
44-
'end: {:d}').format(e.reason, e.start, e.end)
38+
msg = 'Unable to decode input: %s, start: %d, end: %d' % (e.reason, e.start, e.end)
4539
print(msg, file=sys.stderr)
4640
sys.exit(1)
4741

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL