CLI Output
A CLI command can either print its output to a window, or it can return the output as a character string. If the CLI executes a command that returns a string value, it also prints the returned string. Most of the time, you are not concerned with the difference between printing and returning-and-printing. Either way, information is displayed in your window. And, in both cases, printed output is fed through a simple more processor. (This is discussed in more detail in the next section.)
Here are two cases where it matters whether output is printed directly or returned and then printed:
- When the Tcl interpreter executes a list of commands, only the information returned from the last command is printed. Information returned by other commands is not shown.
- You can only assign the output of a command to a variable if the command's output is returned by the command. Output that is printed directly cannot be assigned to a variable or otherwise manipulated unless you save it by using the capture command.
For example, the dload command returns the ID of the process object that was just created. The ID is normally printed--unless, of course, the dload command appears in the middle of a list of commands. For example:
{ dload test_program ; dstatus }
In this case, the CLI does not display the ID of the loaded program since dload was not the last command. On the other hand, you can easily assign the ID of the new process to a variable:
set pid [dload test_program]
In contrast, you cannot assign the output of the help command to a variable. For example, the following does not work:
set htext [help]
This statement assigns an empty string to htext because help does not return text; it just prints information.
To capture the output of a command that prints its output, use the capture command. For example, the following places the output of the help command into a variable:
set htext [capture help]
Note: You can only capture the output from commands. You cannot capture the informational messages displayed by the CLI that describe process state.