| 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 optparse | ||
| 3 | + import argparse | ||
| 4 | 4 | import locale | |
| 5 | 5 | import os | |
| 6 | 6 | import sys | |
@@ -16,29 +16,30 @@ def fatal(msg): | |||
| 16 | 16 | def main(): | |
| 17 | 17 | default_encoding = locale.getpreferredencoding() | |
| 18 | 18 | ||
| 19 | - parser = optparse.OptionParser('%prog [options] [FILE]', | ||
| 19 | + parser = argparse.ArgumentParser( | ||
| 20 | 20 | description="Transliterate Unicode text into ASCII. FILE is path to file to transliterate. " | |
| 21 | 21 | "Standard input is used if FILE is omitted and -c is not specified.") | |
| 22 | - parser.add_option('-e', '--encoding', metavar='ENCODING', default=default_encoding, | ||
| 22 | + parser.add_argument('-e', '--encoding', metavar='ENCODING', default=default_encoding, | ||
| 23 | 23 | help='Specify an encoding (default is %s)' % (default_encoding,)) | |
| 24 | - parser.add_option('-c', metavar='TEXT', dest='text', | ||
| 24 | + parser.add_argument('-c', metavar='TEXT', dest='text', | ||
| 25 | 25 | help='Transliterate TEXT instead of FILE') | |
| 26 | + parser.add_argument('path', nargs='?', metavar='FILE') | ||
| 26 | 27 | ||
| 27 | - options, args = parser.parse_args() | ||
| 28 | + args = parser.parse_args() | ||
| 28 | 29 | ||
| 29 | - encoding = options.encoding | ||
| 30 | + encoding = args.encoding | ||
| 30 | 31 | ||
| 31 | - if args: | ||
| 32 | - if options.text: | ||
| 32 | + if args.path: | ||
| 33 | + if args.text: | ||
| 33 | 34 | fatal("Can't use both FILE and -c option") | |
| 34 | 35 | else: | |
| 35 | - with open(args[0], 'rb') as f: | ||
| 36 | + with open(args.path, 'rb') as f: | ||
| 36 | 37 | stream = f.read() | |
| 37 | - elif options.text: | ||
| 38 | + elif args.text: | ||
| 38 | 39 | if PY3: | |
| 39 | - stream = os.fsencode(options.text) | ||
| 40 | + stream = os.fsencode(args.text) | ||
| 40 | 41 | else: | |
| 41 | - stream = options.text | ||
| 42 | + stream = args.text | ||
| 42 | 43 | # add a newline to the string if it comes from the | |
| 43 | 44 | # command line so that the result is printed nicely | |
| 44 | 45 | # on the console. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments