beetlebug▾
/

namespace ensemble

Makes and configures an ensemble: a command whose subcommands are commands of a namespace.

namespace ensemble create ?OPTION VALUE ...?
namespace ensemble configure COMMAND ?OPTION? ?VALUE ...?
namespace ensemble exists COMMAND

Description

An ensemble is a command whose first argument names a subcommand. Each subcommand is a command, such as a proc of a namespace. string and dict are ensembles.

namespace ensemble create makes the ensemble of the current namespace. Its command has the name of the namespace, unless -command. With no -subcommands and no -map, its subcommands are the exported commands of the namespace, and the list follows a later change of the exports. A unique prefix of a subcommand is the subcommand, unless -prefixes 0.

namespace ensemble configure COMMAND gives each option and its value as a dictionary. With one OPTION it gives its value, and with pairs of an OPTION and a VALUE it sets them. namespace ensemble exists COMMAND gives 1 when COMMAND is an ensemble, and 0 when it is not.

Options

OptionTypeDefaultDescription
-commandcommand namethe name of the namespaceThe name of the command of the ensemble, for create.
-subcommandslist of namesnoneThe subcommands, in place of the exported commands.
-mapdictnoneMaps each subcommand name to a command prefix.
-prefixesboolean1Lets a unique prefix of a subcommand name stand for it.
-parameterslist of namesnoneNames of arguments that come before the subcommand name.
-unknowncommand prefixnoneA handler for a subcommand name that is not known.
-namespacenamespace namenoneThe namespace of the ensemble.

-command NAME

The name of the command of the ensemble, for create.

With create, the ensemble gets the command NAME in place of the name of the namespace.

% namespace eval e2 {proc a {} {return A}; namespace ensemble create -command ::e2cmd -subcommands a}
> e2cmd a
A
% namespace ensemble create -bogus x
bad option "-bogus": must be -map, -namespace, -parameters, -prefixes, -subcommands, or -unknown
-errorcode TCL LOOKUP INDEX option

-subcommands LIST

The subcommands, in place of the exported commands.

Each name of LIST is a subcommand, and it calls the command of the same name in the namespace of the ensemble, exported or not. An empty LIST means the exported commands, or the keys of -map.

% namespace eval e8 {proc a {} {return A}; proc b {} {return B}; namespace ensemble create -subcommands {a}}
> e8 a
A
% e8 b
unknown or ambiguous subcommand "b": must be a
-errorcode TCL LOOKUP SUBCOMMAND b

-map DICT

Maps each subcommand name to a command prefix.

Each key of DICT is a subcommand, and its value is the command prefix that it calls, with the other words of the call after it. With -map and no -subcommands, the subcommands are the keys of DICT.

% namespace eval e3 {proc x {} {return X}; namespace ensemble create -map {y ::e3::x}}
> e3 y
X

% namespace eval e9 {namespace ensemble create -map {up {::string toupper}}}
> e9 up abc
ABC
% e9 down abc
unknown or ambiguous subcommand "down": must be up
-errorcode TCL LOOKUP SUBCOMMAND down

-prefixes BOOLEAN

Lets a unique prefix of a subcommand name stand for it.

With a false value, a call must give the whole name of the subcommand.

% namespace eval e4 {proc go {} {return G}; namespace export go; namespace ensemble create -prefixes 0}
> e4 go
G
% e4 g
unknown subcommand "g": must be go
-errorcode TCL LOOKUP SUBCOMMAND g

-parameters LIST

Names of arguments that come before the subcommand name.

Each name of LIST is one word of the call before the subcommand name. Those words go to the subcommand as its first arguments.

% namespace eval e6 {proc p {x} {return $x}; namespace export p; namespace ensemble create -parameters {x}}
> e6 val p
val
% e6
wrong # args: should be "e6 x subcommand ?arg ...?"
-errorcode TCL WRONGARGS

-unknown PREFIX

A handler for a subcommand name that is not known.

For a call with a name that is not a subcommand, the ensemble calls PREFIX with the name of the ensemble as the call wrote it, the name, and the other words. The handler gives a list: a command prefix to run with the words of the call after the name.

% namespace eval e5 {namespace export *; proc one {} {return 1}; namespace ensemble create -unknown {apply {{ens sub args} {list ::e5::one}}}}
> e5 zzz
1

# The handler puts the name back as the first word of the call of all.
% namespace eval e10 {proc all {args} {return $args}; namespace ensemble create -subcommands all -unknown {apply {{ens sub args} {list ::e10::all $sub}}}}
> e10 zzz q r
zzz q r

Each command prefix is a valid handler.

-namespace NS

The namespace of the ensemble.

Gives the namespace that the ensemble belongs to, the current namespace of create. With configure and a value, it sets the namespace, and the namespace must exist.

% namespace eval e7 {namespace export a; proc a {} {return a}; namespace ensemble create}
> namespace ensemble configure e7 -namespace
::e7
% namespace ensemble configure e7 -namespace ::nope
namespace "::nope" not found
-errorcode NONE

Returns

The name of the ensemble for create, the options or a value for configure, and 1 or 0 for exists.

Examples

% namespace eval jot {proc add {} {return added}; proc list2 {} {return l}; namespace export add list2; namespace ensemble create}
> jot add
added

# A unique prefix of a subcommand.
% jot l
l

% namespace ensemble exists jot
1

% namespace ensemble exists set
0

% namespace ensemble configure jot
-map {} -namespace ::jot -parameters {} -prefixes 1 -subcommands {} -unknown {}

% namespace ensemble configure jot -namespace
::jot

Errors

% jot nope
unknown or ambiguous subcommand "nope": must be add, or list2
-errorcode TCL LOOKUP SUBCOMMAND nope

% jot
wrong # args: should be "jot subcommand ?arg ...?"
-errorcode TCL WRONGARGS

% namespace ensemble bogus
bad subcommand "bogus": must be configure, create, or exists
-errorcode TCL LOOKUP INDEX subcommand

% namespace ensemble configure nope
"nope" is not an ensemble command
-errorcode TCL LOOKUP ENSEMBLE nope

% namespace ensemble configure jot -bogus
bad option "-bogus": must be -map, -namespace, -parameters, -prefixes, -subcommands, or -unknown
-errorcode TCL LOOKUP INDEX option

% namespace ensemble exists
wrong # args: should be "namespace ensemble exists cmdname"
-errorcode TCL WRONGARGS

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also