py/gadict_c5.py
changeset 644 e38cd6112193
parent 618 6ad7203ac9dc
child 646 2d488cfc4c0c
--- a/py/gadict_c5.py	Tue Nov 08 17:44:04 2016 +0200
+++ b/py/gadict_c5.py	Tue Nov 08 18:08:02 2016 +0200
@@ -4,19 +4,51 @@
 import io
 import sys
 import codecs
+import regex
 
 import gadict
 
 
 FINAME = None
 FONAME = None
-if len(sys.argv) >= 2:
-    FINAME = sys.argv[1]
-if len(sys.argv) >= 3:
-    FONAME = sys.argv[2]
 LANGS = None
-if len(sys.argv) >= 4:
-    LANGS = set(sys.argv[3].split(","))
+
+# -lang:ru,uk
+ARG_LANG_RE = regex.compile("-lang:(.+)")
+# -freq:var:TAG=FILE or -freq:freq:TAG=FILE
+ARG_FREQ_RE = regex.compile("-freq:(freq|var):([^=]+)=(.+)")
+
+look_for_files = False
+for idx in range(1, len(sys.argv)):
+    arg = sys.argv[idx]
+    if arg == "--":
+        look_for_files = True
+        continue
+    if not look_for_files:
+        m = ARG_LANG_RE.match(arg)
+        if m:
+            LANGS = set(m.group(1).split(","))
+            for lang in LANGS:
+                if len(lang) != 2:
+                    raise Exception("Incorrect language specification: '{:s}'".format(arg))
+            continue
+        m = ARG_FREQ_RE.match(arg)
+        if m:
+            LANGS = set(arg.split(","))
+            for lang in LANGS:
+                if len(lang) != 2:
+                    raise Exception("Incorrect language specification: '{:s}'".format(arg))
+            continue
+        if arg.startswith("-"):
+            raise Exception("Unsupported option format: '{:s}'".format(arg))
+    if not FINAME:
+        FINAME = arg
+        continue
+    if not FONAME:
+        FONAME = arg
+        continue
+    raise Exception("Unnecessary argument: '{:s}'".format(arg))
+
 
 FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")