Repository: rotki/rotki

Issue:
Providing a non-existing argument should fail gracefully
## Problem Definition

When a non-existing argument is provided to rotkehlchen at the moment  an exception is thrown.

```
__main__.py: error: unrecognized arguments: --lol                                                                                                                                                                                                                               
CRITICAL:root:Traceback (most recent call last):                                                                                                                                                                                                                                
  File "/home/lefteris/w/rotkehlchen/rotkehlchen/__main__.py", line 12, in main
    rotkehlchen_server = RotkehlchenServer()
  File "/home/lefteris/w/rotkehlchen/rotkehlchen/server.py", line 25, in __init__
    self.args = app_args()
  File "/home/lefteris/w/rotkehlchen/rotkehlchen/args.py", line 91, in app_args
    args = p.parse_args()
  File "/usr/lib64/python3.7/argparse.py", line 1752, in parse_args
    self.error(msg % ' '.join(argv))
  File "/usr/lib64/python3.7/argparse.py", line 2501, in error
    self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
  File "/usr/lib64/python3.7/argparse.py", line 2488, in exit
    _sys.exit(status)
SystemExit: 2
```



## Task

Instead of throwing an exception, an error message should be displayed and the program should exit gracefully.


Discussion:

Resolving patch:
diff --git a/rotkehlchen/__main__.py b/rotkehlchen/__main__.py
--- a/rotkehlchen/__main__.py
+++ b/rotkehlchen/__main__.py
@@ -1,6 +1,8 @@
+import logging
+
 from gevent import monkey
+
 monkey.patch_all()
-import logging
 logger = logging.getLogger(__name__)
 
 
@@ -11,8 +13,10 @@ def main():
     try:
         rotkehlchen_server = RotkehlchenServer()
     except SystemExit as e:
-        if e.code is None or e.code == 0:
-            sys.exit(0)
+        if e.code is None or e.code == 0 or e.code == 2:
+            # exit_code 2 is for invalid arguments
+            exit_code = 0 if e.code is None else e.code
+            sys.exit(exit_code)
         else:
             tb = traceback.format_exc()
             logging.critical(tb)
diff --git a/rotkehlchen/args.py b/rotkehlchen/args.py
--- a/rotkehlchen/args.py
+++ b/rotkehlchen/args.py
@@ -1,11 +1,28 @@
 #!/usr/bin/env python
 import argparse
+import sys
+
 from rotkehlchen.config import default_data_directory
+from rotkehlchen.utils import get_system_spec
+
+
+class VersionAction(argparse.Action):
+    def __init__(self, option_strings, dest, nargs=None, required=False, **kwargs):
+        if nargs is not None:
+            raise ValueError("nargs not allowed")
+        super().__init__(option_strings, dest, **kwargs)
+
+    def __call__(self, parser, namespace, values, option_string=None):
+        print(get_system_spec()['rotkehlchen'])
+        sys.exit(0)
 
 
 def app_args() -> argparse.Namespace:
     """ Parse the arguments and create and return the arguments object"""
-    p = argparse.ArgumentParser(description='Rotkehlchen Crypto Portfolio Management')
+    p = argparse.ArgumentParser(
+        prog='rotkehlchen',
+        description='Rotkehlchen Crypto Portfolio Management',
+    )
 
     p.add_argument(
         '--output',
@@ -68,6 +85,11 @@ def app_args() -> argparse.Namespace:
         ),
         action='store_true',
     )
+    p.add_argument(
+        'version',
+        help='Shows the rotkehlchen version',
+        action=VersionAction,
+    )
 
     args = p.parse_args()
 


Test patch: