在我的 TCL 脚本中,我使用了几个没有源代码的过程。所有这些过程都会执行一些任务并输出大量消息。但我只想完成任务,并且想抑制这些消息。有没有办法做到这一点。
例如,我想运行这样的程序:
my_proc $arg1 $arg2 $arg3
并压制所有的消息。任何解决方法/明智的替代方案都会受到赞赏。
更多信息:我正在使用自定义 shell,它接受 TCL 文件作为参数并运行它。在这个自定义 shell 中,我可以访问一些我没有代码的 TCL 程序。
或者甚至有什么方法可以让脚本的输出转到文件而不是命令提示符(stdout)?
尝试改变puts
在你的代码中:
rename ::puts ::tcl_puts
proc puts args {} ;# do nothing
然后,如果你想打印一些东西,使用tcl_puts
这有点像核选择。你可以变得更微妙:
proc puts args {
if {[llength $args] == 1} {
set msg [lindex $args 0]
# here you can filter based on the content, or just ignore it
# ...
} else {
# in the 2a\-args case, it's file io, let that go
# otherwise, it's an error "too many args"
# let Tcl handle it
tcl_puts {*}$args
# should probably to stuff there so that errors look like
# they're coming from "puts", not "tcl_puts"
}
}
另一个想法:只需在您调用的命令期间执行此操作即可:
proc noputs {args} {
rename ::puts ::tcl_puts
proc ::puts args {}
uplevel 1 $args
rename ::puts ""
rename ::tcl_puts ::puts
}
noputs my_proc $arg1 $arg2 $arg3
Demo:
$ tclsh
% proc noputs {args} {
rename ::puts ::tcl_puts
proc ::puts args {}
uplevel 1 $args
rename ::puts ""
rename ::tcl_puts ::puts
}
% proc my_proc {foo bar baz} {
lappend ::my_proc_invocations [list $foo $bar $baz]
puts "in myproc with: $foo $bar $baz"
}
% my_proc 1 2 3
in myproc with: 1 2 3
% noputs my_proc a b c
% my_proc x y z
in myproc with: x y z
% set my_proc_invocations
{1 2 3} {a b c} {x y z}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)