from pathlib import Path
from itertools import count, chain


class Counter():

    def __init__(self, start=0):
        self.count = start
        self.start = start

    def reset(self):
        self.count = self.start

    def next(self):
        self.count += 1
        return self.count


def fetch_file_argument(file_arg):
    """
    Arguments for `file_arg` can either be: "0" which indicates to use the first cli arg, 
    otherwise, the `file_arg` is expected to be a keyword argument like `--data-file`.

    """
    import sys
    args = sys.argv[1:]
    err_msg = f"You must provide a data file argument! ({file_arg})."

    if len(args) == 0:
        raise Exception(err_msg.format(file_arg=file_arg))

    first_arg = True if file_arg == "0" else False

    args_normalized = list(chain(*[a.split('=') for a in args]))

    if first_arg:
        file_arg = args[0]

    elif file_arg in args_normalized:
        file_arg = args_normalized[args_normalized.index(file_arg) + 1]

    else:
        raise Exception(err_msg.format(file_arg=file_arg))

    pth = Path(file_arg)

    if pth.exists():
        return pth

    raise Exception(f'Provided path does not exist! {pth}')
