在我的TCL脚本中,我使用了几个我没有源代码的程序.所有这些过程都执行一些任务并输出大量消息.但我只是希望完成任务,我想要压制消息.有没有办法做到这一点.
所以例如我想运行一个这样的过程:
my_proc $arg1 $arg2 $arg3
Run Code Online (Sandbox Code Playgroud)
并压制它的所有消息.任何变通办法/智能替代方案都值得赞赏.
更多信息:我正在使用一个自定义shell,它将一个TCL文件作为参数并运行它.在这个自定义shell中,我可以访问一些我没有代码的TCL过程.
或者甚至有什么方法可以让脚本的输出转到文件而不是命令提示符(stdout)?
尝试更改puts代码:
rename ::puts ::tcl_puts
proc puts args {} ;# do nothing
Run Code Online (Sandbox Code Playgroud)
然后,如果你想打印一些东西,请使用 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"
}
}
Run Code Online (Sandbox Code Playgroud)
另一个想法:只是在你正在调用的命令期间执行它:
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
Run Code Online (Sandbox Code Playgroud)
演示:
$ 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}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2122 次 |
| 最近记录: |