OSDN Git Service

feat(input option): change getopt to argparse and add -c(output count) input option
authorJohn Chi <john@8btc-1s-MBP.localdomain>
Thu, 14 Jun 2018 09:48:15 +0000 (17:48 +0800)
committerJohn Chi <john@8btc-1s-MBP.localdomain>
Thu, 14 Jun 2018 09:48:15 +0000 (17:48 +0800)
btmtransaction.py
btmvalidation.py
main.py

index 78e3052..85ad884 100644 (file)
@@ -3,15 +3,17 @@ import sys
 import httprequest
 
 miner_fee = 40000000
-max_output = 1500
+max_output_count = 1500
 
 
-def handle_input(_path, _account_id, _password):
+def handle_input(_path, _account_id, _password, _output_count):
+    if _output_count <= 0:
+        _output_count = max_output_count
     lines = list()
     with open(_path, 'r', encoding='utf-8') as file:
         for line in file:
             line = line.strip()
-            if len(lines) < max_output:
+            if len(lines) < _output_count:
                 lines.append(line)
             else:
                 handle_transaction(lines, _path, _account_id, _password)
index 3eb8fe9..55a7944 100755 (executable)
@@ -1,6 +1,6 @@
 import os
 import sys
-import getopt
+import argparse
 import httprequest
 
 
@@ -20,29 +20,18 @@ def validate_amount(line, amount):
         sys.exit(1)
 
 
-def get_input(argv):
-    global _input_path, _account_id, _password
-    try:
-        opts, args = getopt.getopt(argv[1:], 'i:a:p:', ['input=', 'account=', 'password='])
-        if len(opts) < 3:
-            raise getopt.GetoptError('lose command option parameter')
-    except getopt.GetoptError as err:
-        print('Input error:' + str(err) +
-              '\n' + 'Example usage:' +
-              '\n' + '\t./main.py -i input.txt -a 0ETRPAV900A02 -p 123456')
-        sys.exit(1)
-    for opt, arg in opts:
-        if opt in ('-a', '--account'):
-            _account_id = arg
-        if opt in ('-i', '--input'):
-            _input_path = arg
-        if opt in ('-p', '--password'):
-            _password = arg
-    return _input_path, _account_id, _password
-
-
-def validate_input(argv):
-    input_path, account_id, password = get_input(argv)
+def get_input():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-i', required=True, help='transaction input file')
+    parser.add_argument('-a', required=True, help='wallet account id')
+    parser.add_argument('-p', required=True, help='wallet account password')
+    parser.add_argument('-c', type=int, default=0, help='transaction max output count')
+    args = parser.parse_args()
+    return args.i, args.a, args.p, args.c
+
+
+def validate_input():
+    input_path, account_id, password, output_count = get_input()
     # relative path
     file_path = os.path.abspath('.') + os.path.sep + input_path
     if not os.path.exists(file_path):
@@ -55,4 +44,4 @@ def validate_input(argv):
             validate_address(line, splits[0])
             validate_amount(line, splits[1])
         print('Transactions address and amount are valid')
-    return file_path, account_id, password
+    return file_path, account_id, password, output_count
diff --git a/main.py b/main.py
index f6ce312..fe08761 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -5,11 +5,9 @@ import btmvalidation
 import btmtransaction
 
 
-def main(argv=None):
-    if argv is None:
-        argv = sys.argv
-    path, account_id, password = btmvalidation.validate_input(argv)
-    btmtransaction.handle_input(path, account_id, password)
+def main():
+    path, account_id, password, output_count = btmvalidation.validate_input()
+    btmtransaction.handle_input(path, account_id, password, output_count)
 
 
 if __name__ == "__main__":