aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorAdrián Oliva <adrian.oliva@cimat.mx>2023-05-03 19:39:50 -0600
committerAdrián Oliva <adrian.oliva@cimat.mx>2023-05-03 19:39:50 -0600
commite1bbcabed5f39205f891fd4a03c74113dc801d73 (patch)
tree05b49612a0a04c26c0e906cf0020225f2ca04c07 /utils
parent0e3f518fb7bebb5142f1883e913d875094ec3f33 (diff)
downloadLedger.py-e1bbcabed5f39205f891fd4a03c74113dc801d73.tar.gz
Ledger.py-e1bbcabed5f39205f891fd4a03c74113dc801d73.zip
Read and test command line arguments.
First try at reading command line arguments. Still needs testing the command `--sort`.
Diffstat (limited to 'utils')
-rw-r--r--utils/args.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/utils/args.py b/utils/args.py
new file mode 100644
index 0000000..e39d2fe
--- /dev/null
+++ b/utils/args.py
@@ -0,0 +1,72 @@
+import argparse
+import sys
+from os.path import isfile, exists
+
+
+def get_arguments():
+ parser = argparse.ArgumentParser(
+ prog=sys.argv[0],
+ description='A simple implementation of ledger-cli. Supports only the \
+ following commands and flags: `balance`, `register`, `print`, \
+ `--sort`, `--price-db` and `--file`.',
+ epilog='This implementation is inspired in \
+ [ledger-cli](https://ledger-cli.org/).')
+ parser.add_argument('-v', '--version',
+ action='version', version='%(prog)s 0.1')
+
+ parser.add_argument('verb', metavar='Action', action='store',
+ help='Specify an action between balance, report and \
+ print.')
+ parser.add_argument('-S', '--sort', metavar='value-expression',
+ action='store', dest='sort',
+ help='Sort postings by evaluating the given \
+ value-expression.')
+ parser.add_argument('--price-db', metavar='FILE',
+ action='store', dest='price_db',
+ help='Set the FILE that is used for recording \
+ downloaded commodity prices')
+ parser.add_argument('-f', '--file', metavar='FILE',
+ action='append', dest='files',
+ help='Read journal data from FILE.')
+
+ my_args = parser.parse_args()
+ return my_args
+
+
+def test_args(my_args):
+ # Test if verb is valid.
+ valid_verbs = [
+ 'balance', 'bal', 'b',
+ 'register', 'reg', 'r',
+ 'print'
+ ]
+ if my_args.verb not in valid_verbs:
+ raise Exception(f'{my_args.verb} is NOT a valid verb! Valid verbs are: {", ".join(valid_verbs)}.')
+
+ # Test if expression for sorting is valid.
+ # TODO: How can we validate an expression???
+
+ # Test if price_db file exists and is a file.
+ if my_args.price_db and not exists(my_args.price_db):
+ raise Exception(f'Command: `--price-db {my_args.price_db}`. File {my_args.price_db} does NOT exist.')
+ elif my_args.price_db and not isfile(my_args.price_db):
+ raise Exception(f'Command: `--price-db {my_args.price_db}`. {my_args.price_db} is NOT a file.')
+
+ # Test files.
+ if my_args.files:
+ if len(my_args.files) == 1 and my_args.files[0] == '-':
+ # Check if there's data in stdin.
+ #
+ # https://stackoverflow.com/questions/53541223/read-python-stdin-from-pipe-without-blocking-on-empty-input
+ if sys.stdin.isatty():
+ raise Exception(f'Command: --file -. No info from STDIN.')
+
+ else:
+ for file in my_args.files:
+ if not exists(file):
+ raise Exception(f'Command: `--file {file}`. File {file} does NOT exist.')
+ elif not isfile(file):
+ raise Exception(f'Command: `--file {file}`. {file} is NOT a file.')
+ else:
+ if sys.stdin.isatty():
+ raise Exception(f'No info from STDIN. Please specify a file with `--file FILE`.')