def _make_stdin_action(func, err_msg):
"""
Return a StdinAction object that checks for stdin.
func should be the function associated with the parser's -r flag
that is not valid to use with stdin.
"""
class StdinAction(argparse._StoreAction):
def __call__(self, parser, namespace, values, option_string=None):
if values == '-':
if namespace.func == func:
raise argparse.ArgumentError(self, err_msg)
else:
ntf = tempfile.NamedTemporaryFile(delete=False, mode='w')
try:
ntf.write(sys.stdin.read())
finally:
ntf.close()
super(StdinAction, self).__call__(parser,
namespace,
TempPath(ntf.name),
option_string=option_string)
else:
super(StdinAction, self).__call__(parser,
namespace,
values,
option_string=option_string)
return StdinAction
评论列表
文章目录