def get_argparse_extension_checker(choices, dir_allowed=False):
"""Get an :class:`argparge.Action` class that can check for correct extensions.
Returns:
argparse.Action: a class (not an instance) of an argparse action.
"""
class Act(argparse.Action):
def __call__(self, parser, namespace, fname, option_string=None):
is_valid = any(map(lambda choice: fname[-len(choice):] == choice, choices))
if not is_valid and dir_allowed and os.path.isdir(fname):
is_valid = True
if is_valid:
setattr(namespace, self.dest, fname)
else:
option_string = '({})'.format(option_string) if option_string else ''
parser.error("File doesn't end with one of {}{}".format(choices, option_string))
return Act
评论列表
文章目录