def required_length(nmin, nmax):
class RequiredLength(argparse.Action):<EOL><INDENT>def __call__(self, parser, args, values, option_string=None):<EOL><INDENT>if not nmin <= len(values) <= nmax:<EOL><INDENT>msg = '<STR_LIT>'.format(<EOL>f=self.dest, nmin=nmin, nmax=nmax)<EOL>raise argparse.ArgumentTypeError(msg)<EOL><DEDENT>setattr(args, self.dest, values)<EOL><DEDENT><DEDENT>return RequiredLength<EOL>
For use with argparse's action argument. Allows setting a range for nargs.
Example: nargs='+', action=required_length(2, 3)

:param int nmin: Minimum number of arguments
:param int nmax: Maximum number of arguments
:return: RequiredLength object