1
0
PiggyBank/piggybank/cli/main.py

70 lines
2.5 KiB
Python
Raw Normal View History

from re import match
from sys import argv
from piggybank import print_program_version
from piggybank.currencies import print_supported_currencies
USAGE_PUT = "Usage: piggybank put [-r|--reversed] COINS in FILE of CURRENCY\n" \
"Put a set of coins in a piggybank. Set a currency of a new one.\n\n" \
"-r, --reversed -- use reversed order of COINS (from greater to least);\n" \
"COINS -- array of comma or whitespace separated coins;\n" \
"in FILE -- .pb file name of your piggybank;\n" \
"of CURRENCY -- set a currency for a new piggybank.\n"
USAGE_TAKE = "Take a set of coins from a piggybank.\n" \
"Usage: piggybank take [-r|--reversed] COINS from FILE\n\n" \
"-r, --reversed -- use reversed order of COINS (from greater to least);\n" \
"COINS -- array of comma or whitespace separated coins;\n" \
"from FILE -- .pb file of your piggybank.\n" \
USAGE_SHOW = "Show statistics about a piggybank.\n" \
"Usage: piggybank show FILE [with t,transactions]\n\n" \
"FILE -- .pb file name of your piggybank;\n" \
"with t,transaction -- list all transactions.\n"
HELP = "Usage: piggybank [put | take | show] [-v | --version] " \
"[-h | --help] [-L | --list-currencies]\n\n" \
f"{USAGE_PUT}\n" \
f"{USAGE_TAKE}\n" \
f"{USAGE_SHOW}\n\n" \
"-L,--list-currencies -- list all supported currencies and a default one;" \
"-v,--version -- print version of a program;\n" \
"-h,--help -- print this help.\n"
def put(args):
r = r"^put(?P<reversed> -r| --reversed)? (?P<coins>[\d ,]+) in (?P<file>\S+)(?= of (?P<currency>\w+))?"
def take(args):
r = r"^take(?P<reversed> -r| --reversed)? (?P<coins>[\d ,]+) from (?P<file>\S+)"
def show(args):
r = r"^show (?P<file>\S+)(?= with (?P<transactions>t|transactions))?"
def common(args):
r = r"(?P<version>-v|--version)?(?P<help>-h|--help)?(?P<list_currencies>-L|--list-currencies)?"
argd = match(r, args).groupdict()
if not argd["version"] is None:
print_program_version()
exit()
elif not argd["help"] is None:
print(HELP)
elif not argd["list_currencies"] is None:
print_supported_currencies()
def main():
command = argv[1]
args_str = " ".join(argv[1:])
if command == "put":
put(args_str)
elif command == "take":
take(args_str)
elif command == "show":
show(args_str)
else:
common(args_str)
if __name__ == "__main__":
main()