Browse code

Add proper command-line argument parsing.

Xavier G authored on 09/11/2017 22:24:57
Showing 4 changed files

... ...
@@ -5,6 +5,7 @@ import os
5 5
 import sys
6 6
 import json
7 7
 import time
8
+import argparse
8 9
 from prettytable import PrettyTable
9 10
 from termcolor import colored
10 11
 
... ...
@@ -20,11 +21,17 @@ CURRENCIES = {
20 21
     'YEN': '¥'
21 22
 }
22 23
 
24
+def parse_args():
25
+    args_parser = argparse.ArgumentParser(description='Display the contents of cached "boobank list" JSON files.')
26
+    args_parser.add_argument('json_file', nargs='*', default=['-'], help='JSON files to display')
27
+    return args_parser.parse_args()
28
+
23 29
 def main():
30
+    args = parse_args()
24 31
     table = get_table()
25 32
     total = 0
26 33
     timestamps = {}
27
-    filepaths = set(sys.argv[1:])
34
+    filepaths = set(args.json_file)
28 35
     for filepath in filepaths:
29 36
         accounts = get_accounts(filepath)
30 37
         timestamp = accounts.get('timestamp', time.time())
... ...
@@ -8,15 +8,14 @@ import json
8 8
 import copy
9 9
 import time
10 10
 import errno
11
+import argparse
11 12
 import subprocess
12 13
 
13
-CONF_FILE = '~/.config/boobank-cacher/conf.json'
14
+DEFAULT_CONF_FILE = '~/.config/boobank-cacher/conf.json'
14 15
 
15
-def get_conf_path():
16
-    return os.path.expanduser(CONF_FILE)
17
-
18
-def get_conf():
19
-    with open(get_conf_path(), 'r') as filedesc:
16
+def get_conf(conf_path):
17
+    conf_path = os.path.expanduser(conf_path)
18
+    with open(conf_path, 'r') as filedesc:
20 19
         return json.load(filedesc)
21 20
 
22 21
 def mkdir(path, mode):
... ...
@@ -83,8 +82,14 @@ def write_cache(results):
83 82
         with open(cache_path, 'w') as filedesc:
84 83
             json.dump(command['result'], filedesc, indent=4)
85 84
 
85
+def parse_args():
86
+    args_parser = argparse.ArgumentParser(description='Cache the output of boobank commands to JSON files.')
87
+    args_parser.add_argument('-c', '--config', default=DEFAULT_CONF_FILE, help='configuration file to use')
88
+    return args_parser.parse_args()
89
+
86 90
 def main():
87
-    conf = get_conf()
91
+    args = parse_args()
92
+    conf = get_conf(args.config)
88 93
     results = run_boobank(conf)
89 94
     process_additions(results)
90 95
     write_cache(results)
... ...
@@ -5,38 +5,34 @@ import sys
5 5
 import json
6 6
 import time
7 7
 import socket
8
+import argparse
8 9
 
9 10
 
10 11
 def main():
11
-    accounts = get_accounts()
12
+    args = parse_args()
13
+    accounts = get_accounts(args.json_file)
12 14
     timestamp = time.time()
13
-    socket = graphite_connect()
15
+    socket = graphite_connect(args.host, args.port)
14 16
     for account in accounts.get('data', []):
15 17
         send_account(socket, account, timestamp)
16 18
     socket.close()
17 19
 
18
-def graphite_connect():
19
-    (host, port) = ('::1', 2003)
20
-    if len(sys.argv) == 4:
21
-        (host, port) = (sys.argv[2], int(sys.argv[3]))
22
-    elif len(sys.argv) == 3:
23
-        host = sys.argv[2]
20
+def parse_args():
21
+    args_parser = argparse.ArgumentParser(description='Send the contents of a boobank-cacher JSON file to Graphite.')
22
+    args_parser.add_argument('-gh', '--graphite-host', dest='host', default='::1', help='Graphite host; defaults to ::1.')
23
+    args_parser.add_argument('-gp', '--graphite-port', dest='port', default='2003', type=int, help='Graphite port; defaults to 2003.')
24
+    args_parser.add_argument('json_file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='JSON file to send to Graphite; defaults to "-" (standard input).')
25
+    return args_parser.parse_args()
26
+
27
+def graphite_connect(host, port):
24 28
     addr_infos = socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP)
25 29
     _, _, _, _, sockaddr = addr_infos[0]
26 30
     graphite_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
27 31
     graphite_socket.connect(sockaddr)
28 32
     return graphite_socket
29 33
 
30
-def get_accounts():
31
-    try:
32
-        accounts_json_path = sys.argv[1]
33
-    except IndexError:
34
-        accounts_json_path = '-'
35
-    if accounts_json_path == '-':
36
-        return json.load(sys.stdin)
37
-    else:
38
-        with open(accounts_json_path, 'r') as filedesc:
39
-            return json.load(filedesc)
34
+def get_accounts(filedesc):
35
+    return json.load(filedesc)
40 36
 
41 37
 def get_path(account):
42 38
     (id, bank) = account['id'].split('@')
... ...
@@ -5,6 +5,7 @@ import os
5 5
 import sys
6 6
 import json
7 7
 import time
8
+import argparse
8 9
 from prettytable import PrettyTable
9 10
 from termcolor import colored
10 11
 
... ...
@@ -17,9 +18,16 @@ CURRENCIES = {
17 18
     'YEN': '¥'
18 19
 }
19 20
 
21
+def parse_args():
22
+    args_parser = argparse.ArgumentParser(description='Display the contents of a cached "boobank history" JSON file.')
23
+    args_parser.add_argument('json_file', nargs='?', default='-', help='JSON file to display')
24
+    args_parser.add_argument('-c', '--count', default=None, type=int, help='Maximum number of operations to display.')
25
+    return args_parser.parse_args()
26
+
20 27
 def main():
21
-    filepath = sys.argv[1]
22
-    max_operations = int(sys.argv[2]) if len(sys.argv) > 2 else None
28
+    args = parse_args()
29
+    filepath = args.json_file
30
+    max_operations = args.count
23 31
     history = get_history(filepath)
24 32
     timestamp = history.get('timestamp', time.time())
25 33
     table = get_table(timestamp)