[docs]deftee(args,stdin,stdout,stderr):"""A tee command for xonsh."""mode="w"if"-a"inargs:args.remove("-a")mode="a"if"--append"inargs:args.remove("--append")mode="a"if"--help"inargs:print(TEE_HELP,file=stdout)return0ifstdinisNone:msg="tee was not piped stdin, must have input stream to read from."print(msg,file=stderr)return1errors=Falsefiles=[]foriinargs:ifi=="-":files.append(stdout)else:try:files.append(open(i,mode))except:print(f"tee: failed to open {i}",file=stderr)errors=Truefiles.append(stdout)whileTrue:r=stdin.read(1024)ifr=="":breakforiinfiles:i.write(r)foriinfiles:ifi!=stdout:i.close()returnint(errors)
TEE_HELP="""This version of tee was written in Python for the xonsh project: http://xon.shBased on tee from GNU coreutils: http://www.gnu.org/software/coreutils/Usage: tee [OPTION]... [FILE]...Copy standard input to each FILE, and also to standard output. -a, --append append to the given FILEs, do not overwrite --help display this help and exitIf a FILE is -, copy again to standard output."""# NOT IMPLEMENTED:# -i, --ignore-interrupts ignore interrupt signals