from typing import Sequence
from typing import Optional
from typing import Tuple


def separate_subargs_under_name(args: Sequence[str], name: str, short_name: Optional[str] = None) -> Tuple[Sequence[str], Sequence[str]]:
    """
    Separate out args with the given under the given name.  E.g.
        separate_args_with_prefix(['--greeting',  'hello', '--person.name', 'Suzy', '--person.age', '30'], prefix='person')
            == ['--name', 'Suzy', '--age', '30'], ['--greeting',  'hello']
    :param args:
    :param name: The arg
    :param short_name: Optionall, the shortened prefix, e.g. 'p'
    :return:
    """
    keepers = []
    remainders = []
    last_was_keeper = False
    for argstr in args:
        is_argument_key = argstr.startswith('-') and len(argstr.lstrip('-.,0123456789')) != 0
        if is_argument_key:
            full_prefix = f'--{name}.'
            short_prefix = f'-{short_name}.' if short_name is not None else None
            if argstr.startswith(full_prefix):
                subarg = argstr[len(full_prefix):]
                keepers.append('--' + subarg)
                last_was_keeper = True
            elif short_prefix is not None and argstr.startswith(short_prefix):
                subarg = argstr[len(short_prefix):]
                keepers.append('--' + subarg)
                last_was_keeper = True
            else:
                remainders.append(argstr)
                last_was_keeper = False
        elif last_was_keeper:
            keepers.append(argstr)
            last_was_keeper = False
        else:
            remainders.append(argstr)

    return keepers, remainders