如何抑制TCL过程的输出消息?

如何抑制TCL过程的输出消息?,tcl,Tcl,在我的TCL脚本中,我使用了几个没有源代码的过程。所有这些过程都执行一些任务并输出大量消息。但我只想完成任务,我想抑制消息。有没有办法做到这一点 例如,我想运行这样一个过程: my_proc $arg1 $arg2 $arg3 并抑制它的所有消息。任何变通方法/智能替代方案都值得赞赏 更多信息:我正在使用一个自定义shell,它将TCL文件作为参数并运行它。在这个定制shell中,我可以访问一些没有代码的TCL过程 甚至有没有办法让脚本的输出转到文件而不是命令提示符stdout?尝试更改代码中

在我的TCL脚本中,我使用了几个没有源代码的过程。所有这些过程都执行一些任务并输出大量消息。但我只想完成任务,我想抑制消息。有没有办法做到这一点

例如,我想运行这样一个过程:

my_proc $arg1 $arg2 $arg3
并抑制它的所有消息。任何变通方法/智能替代方案都值得赞赏

更多信息:我正在使用一个自定义shell,它将TCL文件作为参数并运行它。在这个定制shell中,我可以访问一些没有代码的TCL过程

甚至有没有办法让脚本的输出转到文件而不是命令提示符stdout?

尝试更改代码中的输入:

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
演示:


你能做什么:设置输出[my_proc$args]?@Glenn我试过了,它仍然打印东西。我想在proc代码中有一大堆puts语句是我无法访问的。我在想shell。我的评论很愚蠢。很适合使用try…最终在8.6中
$ 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}