| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | # vim:ts=4 sw=4 expandtab softtabstop=4 | |
| 2 | 2 | from __future__ import print_function | |
| 3 | - import argparse | ||
| 3 | + import optparse | ||
| 4 | 4 | import locale | |
| 5 | 5 | import os | |
| 6 | 6 | import sys | |
@@ -11,25 +11,20 @@ | |||
| 11 | 11 | PY3 = sys.version_info[0] >= 3 | |
| 12 | 12 | ||
| 13 | 13 | 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 | ||
| 30 | 25 | ||
| 31 | - if args.file: | ||
| 32 | - with open(args.file, 'rb') as f: | ||
| 26 | + if args: | ||
| 27 | + with open(args[0], 'rb') as f: | ||
| 33 | 28 | stream = f.read() | |
| 34 | 29 | else: | |
| 35 | 30 | if PY3: | |
@@ -40,8 +35,7 @@ def main(): | |||
| 40 | 35 | try: | |
| 41 | 36 | stream = stream.decode(encoding) | |
| 42 | 37 | 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) | ||
| 45 | 39 | print(msg, file=sys.stderr) | |
| 46 | 40 | sys.exit(1) | |
| 47 | 41 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments