from re import search from typing import List, Callable from sys import argv, exit from piggybank import print_program_version from piggybank.configuration import Configuration from piggybank.currencies import CURRENCIES __all__ = ["handle_default_arguments", "complement_array_of_coins"] USAGE_COMMON: str = "Usage: piggybank-* " \ "[(-h|--help)|(-v|--version)|(-L|--list-currencies)]\n" \ "A set of common flags. Only one could be specified.\n" \ "-h, --help -- print this help;\n" \ "-v, --version -- print program version;\n" \ "-L, --list-currencies -- print supported currencies;\n" \ "--set-default-currency CURRENCY -- set default currency.\n" def parse_common_arguments(args: str): r = r"(?P-h|--help)|(?P-v|--version)" \ r"|(?P-L|--list-currencies)" \ r"|(?=--set-default-currency (?P\w+))" argd = search(r, args) if not argd is None: argd = argd.groupdict() return { "help": argd["help"] is None, "version": argd["transactions"] is None, "list-currencies": argd["list_currencies"] is None, "default-currency": argd["default_currency"] } def handle_default_arguments(args: dict, help_func: Callable) -> None: cargs = parse_common_arguments(' '.join(argv)) if cargs["help"]: help_func() print(USAGE_COMMON) exit() elif cargs["version"]: print_program_version() exit() elif cargs["list-currencies"]: print_supported_currencies() exit() elif not cargs["default-currency"] is None: Configuration()["default-currency"] = cargs["default-currency"] exit() def print_supported_currencies() -> None: """Print a list of supported currencies.""" print("Supported currencies are:") for cur in CURRENCIES: print(f" {cur:^4} ┃ {CURRENCIES[cur]['name']:^31}" f" ┃ {CURRENCIES[cur]['description']}") print("Default currency is", Configuration()["default-currency"]) def complement_array_of_coins(coins: List[int], currency: str, _reversed: bool = False) -> List[int]: """Complements array of coins up to the count of currency's coins.""" offset_array = [0] * (CURRENCIES[currency]["count"] - len(coins)) return offset_array + coins if not _reversed else coins + offset_array