Commits

spirit committed 367de04

file argument

  • Participants
  • Parent commits 62bc140

Comments (0)

Files changed (1)

File guess_language/__main__.py

 
 import guess_language
 
+DEFAULT_ENCODING = "utf-8"
+
 
 def parse_args():
     parser = argparse.ArgumentParser(
         prog="{} -m {}".format(os.path.basename(sys.executable),
                                "guess_language")
     )
+    parser.add_argument("file", nargs="?", help="plain text file")
     parser.add_argument("--encoding", dest="encoding", help="input encoding")
-    parser.add_argument("--no-enchant", dest="use_enchant",
-                        action="store_false", help="don't use enchant")
+    parser.add_argument("--disable-enchant", dest="use_enchant",
+                        action="store_false", help="disable enchant")
     return parser.parse_args()
 
 
 def main():
     args = parse_args()
-    encoding = (args.encoding if args.encoding
-                else sys.stdin.encoding if sys.stdin.isatty() else "utf-8")
-    with open(sys.stdin.fileno(), encoding=encoding) as f:
+    if args.file:
+        file = args.file
+        encoding = args.encoding if args.encoding else DEFAULT_ENCODING
+    else:
+        file = sys.stdin.fileno()
+        encoding = args.encoding if args.encoding else sys.stdin.encoding
+    with open(file, encoding=encoding) as f:
         text = "\n".join(f.readlines())
     guess_language.USE_ENCHANT = args.use_enchant
     tag = guess_language.guess_language(text)