summaryrefslogtreecommitdiff
path: root/unidecode
diff options
context:
space:
mode:
authorTomaz Solc <tomaz.solc@tablix.org>2019-01-19 11:32:03 +0100
committerTomaz Solc <tomaz.solc@tablix.org>2019-01-19 11:32:03 +0100
commitd31c81503a112e3e0161ae55ba6a33a149cfc892 (patch)
tree756d25cb77c525173577b18c56179c9f5e33673a /unidecode
parent6c96492dbfe8e2cb8eee5024734a7628b5db7476 (diff)
parent62a7e521ed35adb48b7afaf2b63a0f16a4a8ebbe (diff)
downloadunidecode-d31c81503a112e3e0161ae55ba6a33a149cfc892.tar.gz
Merge remote-tracking branch 'jdufresne/argparse'
Diffstat (limited to 'unidecode')
-rw-r--r--unidecode/util.py25
1 files changed, 13 insertions, 12 deletions
diff --git a/unidecode/util.py b/unidecode/util.py
index 982e0cd..6abd4ce 100644
--- a/unidecode/util.py
+++ b/unidecode/util.py
@@ -1,6 +1,6 @@
# vim:ts=4 sw=4 expandtab softtabstop=4
from __future__ import print_function
-import optparse
+import argparse
import locale
import os
import sys
@@ -16,29 +16,30 @@ def fatal(msg):
def main():
default_encoding = locale.getpreferredencoding()
- parser = optparse.OptionParser('%prog [options] [FILE]',
+ parser = argparse.ArgumentParser(
description="Transliterate Unicode text into ASCII. FILE is path to file to transliterate. "
"Standard input is used if FILE is omitted and -c is not specified.")
- parser.add_option('-e', '--encoding', metavar='ENCODING', default=default_encoding,
+ parser.add_argument('-e', '--encoding', metavar='ENCODING', default=default_encoding,
help='Specify an encoding (default is %s)' % (default_encoding,))
- parser.add_option('-c', metavar='TEXT', dest='text',
+ parser.add_argument('-c', metavar='TEXT', dest='text',
help='Transliterate TEXT instead of FILE')
+ parser.add_argument('path', nargs='?', metavar='FILE')
- options, args = parser.parse_args()
+ args = parser.parse_args()
- encoding = options.encoding
+ encoding = args.encoding
- if args:
- if options.text:
+ if args.path:
+ if args.text:
fatal("Can't use both FILE and -c option")
else:
- with open(args[0], 'rb') as f:
+ with open(args.path, 'rb') as f:
stream = f.read()
- elif options.text:
+ elif args.text:
if PY3:
- stream = os.fsencode(options.text)
+ stream = os.fsencode(args.text)
else:
- stream = options.text
+ stream = args.text
# add a newline to the string if it comes from the
# command line so that the result is printed nicely
# on the console.