Support command line invocation

Effectively this opens the KorAP API also for shell scripts.

Change-Id: I10181f7882f07ef1d9dc12e572b387af9f79fcba
diff --git a/KorAPClient/__main__.py b/KorAPClient/__main__.py
new file mode 100644
index 0000000..222f3c8
--- /dev/null
+++ b/KorAPClient/__main__.py
@@ -0,0 +1,30 @@
+import argparse
+
+from KorAPClient import KorAPConnection
+
+prog = None if globals().get('__spec__') is None else 'python -m {}'.format(__spec__.name.partition('.')[0])
+example = "example:\n  " + prog + ' -v --query "Hello World" "Hallo Welt" --vc "pubDate in 2017" "pubDate in 2018" "pubDate in 2019"'
+parser = argparse.ArgumentParser(
+    prog=prog,
+    epilog=example,
+    formatter_class=argparse.RawDescriptionHelpFormatter,
+    description='Send a query to the KorAP API and print results as tsv.')
+parser.add_argument('-v', '--verbose', action="store_true")
+parser.add_argument('-l', '--query-language', default="poliqarp")
+parser.add_argument('-u', '--api-url', default=None, help="Specify this to access a corpus other that DeReKo. ")
+parser.add_argument("-c", '--vc', nargs='+', help='virtual corpus definition[s]', default=[""])
+parser.add_argument('-q', '--query', nargs="+", help='If not specified only the size of the virtual '
+                                                     'corpus will be queried.')
+args = parser.parse_args()
+
+if __name__ == "__main__":
+    if args.api_url is None:
+        kcon = KorAPConnection(verbose=args.verbose)
+    else:
+        kcon = KorAPConnection(apiUrl=args.api_url, verbose=args.verbose)
+    if args.query:
+        df = kcon.frequencyQuery(query=args.query if len(args.query) > 1 else args.query[0],
+                                 vc=args.vc if len(args.vc) > 1 else args.vc[0], ql=args.query_language)
+    else:
+        df = kcon.corpusStats(vc=args.vc if len(args.vc) > 1 else args.vc[0], **{"as.df": True})
+    print(df.to_csv(sep="\t"))
diff --git a/Readme.md b/Readme.md
index c96fe6d..ce36ff4 100644
--- a/Readme.md
+++ b/Readme.md
@@ -6,6 +6,7 @@
 [![GitHub closed issues](https://img.shields.io/github/issues-raw/KorAP/PythonKorAPClient.svg)](https://github.com/KorAP/PythonKorAPClient/issues)
 [![GitHub issues](https://img.shields.io/github/issues-closed-raw/KorAP/PythonKorAPClient.svg)](https://github.com/KorAP/PythonKorAPClient/issues)
 [![GitHub license](https://img.shields.io/github/license/KorAP/PythonKorAPClient)](https://github.com/KorAP/PythonKorAPClient/blob/master/LICENSE)
+[![HitCount](http://hits.dwyl.com/KorAP/PythonKorAPClient.svg)](http://hits.dwyl.com/KorAP/PythonKorAPClient)
 ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/KorAPClient)
 ![PyPI - Downloads](https://img.shields.io/pypi/dm/KorAPClient)
 ## Description
@@ -76,6 +77,28 @@
 ```
 ![Frequency per million words of “Hello World“ in DE vs. AT from 2010 to 2018 in newspapers and magazines](figures/hello-world.png)
 
+### Command Line Invocation
+The Python KorAP client can also be called from the command line.
+```shell script
+$ korapclient -h
+usage: python -m KorAPClient [-h] [-v] [-l QUERY_LANGUAGE] [-u API_URL] [-c VC [VC ...]] [-q QUERY [QUERY ...]]
+
+Send a query to the KorAP API and print results as tsv.
+
+optional arguments:
+  -h, --help            show this help message and exit
+  -v, --verbose
+  -l QUERY_LANGUAGE, --query-language QUERY_LANGUAGE
+  -u API_URL, --api-url API_URL
+                        Specify this to access a corpus other that DeReKo.
+  -c VC [VC ...], --vc VC [VC ...]
+                        virtual corpus definition[s]
+  -q QUERY [QUERY ...], --query QUERY [QUERY ...]
+                        If not specified only the size of the virtual corpus will be queried.
+
+example:
+  python -m KorAPClient -v --query "Hello World" "Hallo Welt" --vc "pubDate in 2017" "pubDate in 2018" "pubDate in 2019"
+```
 ### Accessed API Services
 By using the KorAPClient you agree to the respective terms of use of the accessed KorAP API services which will be printed upon opening a connection.
 
@@ -111,3 +134,4 @@
 - Kupietz, Marc / Margaretha, Eliza / Diewald, Nils / Lüngen, Harald / Fankhauser, Peter (2019): [What’s New in EuReCo? Interoperability, Comparable Corpora, Licensing](https://nbn-resolving.org/urn:nbn:de:bsz:mh39-90261). In: BaƄski, Piotr/Barbaresi, Adrien/Biber, Hanno/Breiteneder, Evelyn/Clematide, Simon/Kupietz, Marc/Lüngen, Harald/Iliadi, Caroline (eds.): [*Proceedings of the International Corpus Linguistics Conference 2019 Workshop "Challenges in the Management of Large Corpora (CMLC-7)"*](https://ids-pub.bsz-bw.de/solrsearch/index/search/searchtype/collection/id/21038), 22nd of July Mannheim: Leibniz-Institut für Deutsche Sprache, 33-39.
 
 - Kupietz, Marc / Diewald, Nils / Margaretha, Eliza (2020): [RKorAPClient: An R package for accessing the German Reference Corpus DeReKo via KorAP](http://www.lrec-conf.org/proceedings/lrec2020/pdf/2020.lrec-1.867.pdf). In: Calzolari, Nicoletta, Frédéric Béchet, Philippe Blache, Khalid Choukri, Christopher Cieri,  Thierry Declerck, Sara Goggi, Hitoshi Isahara, Bente Maegaard, Joseph Mariani, Hélène Mazo, Asuncion Moreno, Jan Odijk, Stelios Piperidis (eds.): [Proceedings of The 12th Language Resources and Evaluation Conference (LREC 2020)](http://www.lrec-conf.org/proceedings/lrec2020/LREC-2020.pdf). Marseille: European Language Resources Association (ELRA), 7017-7023.
+
diff --git a/bin/korapclient b/bin/korapclient
new file mode 100755
index 0000000..70413fe
--- /dev/null
+++ b/bin/korapclient
@@ -0,0 +1,2 @@
+#!/bin/sh
+/usr/bin/env python3 -m KorAPClient "$@"
diff --git a/setup.py b/setup.py
index 65d92ed..b60cbac 100644
--- a/setup.py
+++ b/setup.py
@@ -24,6 +24,7 @@
         'Intended Audience :: Developers',
         'Intended Audience :: Science/Research',
     ],
+    scripts=['bin/korapclient'],
     test_suite='nose.collector',
     tests_require=['nose'],
     python_requires='>=3.6',