|
| 1 | +An ensemble is a single command that can dispatch subcommands |
| 2 | +to other commands. |
| 3 | + |
| 4 | +For example [string] is a built-in ensemble. |
| 5 | + |
| 6 | +The ensemble command allows an ensemble command to be created |
| 7 | +that redirects to other commands. |
| 8 | + |
| 9 | +Create an ensemble by having multiple commands that all share |
| 10 | +the same prefix. For example: |
| 11 | + |
| 12 | +proc {test open} {name} { ... } |
| 13 | +proc {test close} {handle} { ... } |
| 14 | +proc {test show} {handle} { ... } |
| 15 | + |
| 16 | +Then simply: |
| 17 | + |
| 18 | +ensemble test |
| 19 | + |
| 20 | +Now a new command, test, is created that will invoke the other commands |
| 21 | +based on the first argument. For example: |
| 22 | + |
| 23 | +set h [test open file.txt] |
| 24 | +test show $h |
| 25 | +test close $h |
| 26 | + |
| 27 | +By default ensemble expects the commands to be named "<name> ". If another |
| 28 | +prefix is used, this can be specified with the -automap option. e.g. |
| 29 | + |
| 30 | +ensemble test -automap test. |
| 31 | + |
| 32 | +This could be used if the commands were named test.open, test.close, test.show |
| 33 | + |
| 34 | +Note that ensembles are dynamic, not fixed at the point of creation. |
| 35 | +This means, for example, that we can can create a new commands, "test reverse" |
| 36 | +after the ensemble has been created and it can still be invoked as test reverse ... |
| 37 | + |
| 38 | +It is easy to create an ensemble for commands in a namespace by simply using |
| 39 | +-automap <ns>:: however for compatibility with Tcl, 'namespace ensemble create' is provided |
| 40 | +that does with when invoked within a namespace. e.g. |
| 41 | + |
| 42 | +namespace eval test { |
| 43 | + namespace ensemble create |
| 44 | + |
| 45 | + proc open {name} { ... } |
| 46 | + proc close {handle} { ... } |
| 47 | + proc show {handle} { ... } |
| 48 | +} |
| 49 | + |
| 50 | +test open file.txt |
0 commit comments