'How to stop tcl command from executing when undesired options are used and then use a proc to correct the options to apply the updated command?
For example, say, the command of interest is puts. I would like to include -nonewline option when the input string is "foo".
So when the user enters %puts foo, I would like to instead apply puts -nonewline foo.
The above is just a general example of what I would like to achieve.
I would like to not change the proc or source code, or use alias.
Instead, I want to be able to do the following (using the above example):
- do not execute %puts foo.
- get user entered command, namely "puts foo". -> parse this through some proc and add "-nonewline".
- execute the updated command.
High-level description for general built-in commands:
- stop user-entered command in command-line if it contains undesired options/strings/etc. (Q: does it matter if it's user-entered or if it's a sourced script?)
- correct/update the user-entered command
- execute the corrected/updated command from 2)
fyi: I am trying to do so for EDA tool's built-in commands.
I tried using 'trace add execution' which allows you to execute proc/command when a specific command is called, but it executes the command regardless.
Solution 1:[1]
The usual way to do this kind of thing in Tcl is to rename the command you want to modify and then redefine the original name as a proc which does the extra processing and then calls the original command. For details see https://wiki.tcl-lang.org/page/wrapping+commands .
So for your example with puts you would do:
rename puts puts.orig
proc puts args {
if {[llength $args] == 1 && [lindex $args 0] eq "foo"} {
puts.orig -nonewline "foo"
} else {
puts.orig {*}$args
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
