def check_arg(arg_name, arg_val, required_type, allow_none=True, min=None,
    max=None, choices=[]):
    """Check argument values and type"""
    if allow_none and arg_val is None:
        return arg_val
    if not isinstance(arg_val, required_type):
        try:
            arg_val = required_type(arg_val)
        except:
            raise Exception(
                'Argument `{}` value `{}` is not in correct type: `{}` and cannot be coerced'
                .format(arg_name, arg_val, required_type.__name__))
    if required_type in [float, int]:
        if min and arg_val < min:
            raise Exception(
                'Argument `{}` value `{}` is too low. Minimal value: {}'.
                format(arg_name, arg_val, min))
        if max and arg_val > max:
            raise Exception(
                'Argument `{}` value `{}` is too high. Maximal value: {}'.
                format(arg_name, arg_val, max))
    if choices and arg_val not in choices:
        raise Exception(
            'Argument `{}` value `{}` is not in the list of possible choices. Choices: {}'
            .format(arg_name, arg_val, ', '.join(choices)))
    return arg_val
