Skip to content

Commit bce834d

Browse files
committed
Use locale encoding when piping stdin.
1 parent 99c717c commit bce834d

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

‎language_tool/__main__.py‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33

44
import argparse
5+
import locale
56
import os
67
import re
78
import sys
@@ -17,8 +18,8 @@ def parse_args():
1718
prog="{} -m {}".format(os.path.basename(sys.executable),
1819
"language_tool")
1920
)
20-
parser.add_argument("file", nargs="?",
21-
help="plain text file")
21+
parser.add_argument("file",
22+
help='plain text file or "-" for stdin')
2223
parser.add_argument("--encoding",
2324
help="input encoding")
2425
parser.add_argument("--language", metavar="CODE",
@@ -53,12 +54,15 @@ def main():
5354
print("LanguageTool {}".format(language_tool.get_version()))
5455
return
5556

56-
if args.file:
57-
file = args.file
58-
encoding = args.encoding if args.encoding else DEFAULT_ENCODING
59-
else:
57+
if args.file == "-":
6058
file = sys.stdin.fileno()
61-
encoding = args.encoding if args.encoding else sys.stdin.encoding
59+
encoding = args.encoding or (
60+
sys.stdin.encoding if sys.stdin.isatty()
61+
else locale.getpreferredencoding()
62+
)
63+
else:
64+
file = args.file
65+
encoding = args.encoding or "utf-8"
6266

6367
lang_tool = language_tool.LanguageTool(motherTongue=args.mother_tongue)
6468
guess_language = None

0 commit comments

Comments
 (0)