def add_argument(parser, flag, type=None, **kwargs):
    """Wrapper to add arguments to an argument parser. Fixes argparse's
    behavior with type=bool. For a bool flag 'test', this adds options '--test'
    which by default sets test to on, and additionally supports '--test true',
    '--test false' and so on. Finally, 'test' can also be turned off by simply
    specifying '--notest'.
    """
    def str2bool(v):
        return v.lower() in ('true', 't', '1')
    if flag.startswith('-'):
        raise ValueError('Flags should not have the preceeding - symbols, -- will be added automatically.')
    if type == bool:
        parser.add_argument('--' + flag, type=str2bool, nargs='?', const=True, **kwargs)
        parser.add_argument('--no' + flag, action='store_false', dest=flag)
    else:
        parser.add_argument('--' + flag, type=type, **kwargs)