option(3itcl)

✨ option(3itcl) ✨

part of the incr Tcl family, bestie πŸ’–


🌸 NAME 🌸

itcl::option β€” define options for extendedclass, widget or widgetadaptor

ngl some chunks of this description are "borrowed" from the Tcl extension [snit], because the functionality is mostly identical. credit where credit's due πŸ’…

⚠️ WARNING! ⚠️

okay so this is new functionality in [incr Tcl] where the API can still change!! like bestie do not build your whole personality on this yet 😭

πŸ¦‹ SYNOPSIS πŸ¦‹

option optionSpec ?defaultValue?
option optionSpec ?options?

πŸŽ€ DESCRIPTION πŸŽ€

So the option command is what you use inside an [incr Tcl] extendedclass/widget/widgetadaptor definition to define options. it's giving interior design for your class ✨

In the first form, it defines an option for instances of this type, and optionally gives it an initial value. If you don't specify a defaultValue, the initial value defaults to the empty string β€” no cap, just "".

An option defined this way is said to be locally defined. The optionSpec is a list defining the option's name, resource name, and class name, like:

option {-font font Font} {Courier 12}

The option name must begin with a hyphen, and must not contain any upper case letters. lowercase only, it's a vibe πŸ’…

The resource name and class name are optional; if you don't specify them, the resource name defaults to the option name minus the hyphen, and the class name defaults to the resource name with the first letter capitalized. So the following statement is literally equivalent to the previous example:

option -font {Courier 12}

See The Tk Option Database for more info about resource and class names. 🫢

Options are normally set and retrieved using the standard instance methods configure and cget; and within instance code (method bodies, etc.), option values are available through the options array:

set myfont $itcl_options(-font)

In the second form you can define option handlers (e.g., -configuremethod), then it should probably use configure and cget to access its options to avoid subtle errors. (subtle errors are NOT the moment 😭)

The option statement may include the following options:

  • -default defvalue β€” Defines the option's default value; the option's default value will be "" otherwise. so if you skip this, it's empty-string szn 🌸
  • -readonly β€” The option is handled read-only β€” it can only be set using configure at creation time, i.e., in the type's constructor. after that? hands off bestie πŸ™ˆ
  • -cgetmethod methodName β€” Every locally-defined option may define a -cgetmethod; it is called when the option's value is retrieved using the cget method. Whatever the method's body returns will be the return value of the call to cget.

The named method must take one argument, the option name. For example, this code is equivalent to (though slower than) Itcl's default handling of cget:

option -font -cgetmethod GetOption
method GetOption {option} {
    return $itcl_options($option)
}

Note that it's possible for any number of options to share a -cgetmethod. sharing is caring 🫢

  • -cgetmethodvar varName β€” That is very similar to -cgetmethod, the only difference is, one can define a variable, where to find the cgetmethod during runtime. ✨ dynamic queen behavior ✨
  • -configuremethod methodName β€” Every locally-defined option may define a -configuremethod; it is called when the option's value is set using the configure or configurelist methods. It is the named method's responsibility to save the option's value; in other words, the value will not be saved to the itcl_options() array unless the method saves it there. so like, if you forget, it's gone. poof. πŸ’€

The named method must take two arguments, the option name and its new value. For example, this code is equivalent to (though slower than) Itcl's default handling of configure:

option -font -configuremethod SetOption
method SetOption {option value} {
    set itcl_options($option) $value
}

Note that it's possible for any number of options to share a single -configuremethod.

  • -configuremethodvar varName β€” That is very similar to -configuremethod, the only difference is, one can define a variable, where to find the configuremethod during runtime.
  • -validatemethod methodName β€” Every locally-defined option may define a -validatemethod; it is called when the option's value is set using the configure or configurelist methods, just before the -configuremethod (if any). It is the named method's responsibility to validate the option's new value, and to throw an error if the value is invalid. gatekeeping your own options, iconic honestly πŸ‘‘

The named method must take two arguments, the option name and its new value. For example, this code verifies that -flag's value is a valid Boolean value:

option -font -validatemethod CheckBoolean
method CheckBoolean {option value} {
    if {![string is boolean -strict $value]} {
        error "option $option must have a boolean value."
    }
}

Note that it's possible for any number of options to share a single -validatemethod.

  • -validatemethodvar varName β€” That is very similar to -validatemethod, the only difference is, one can define a variable, where to find the validatemethod during runtime.

🌈 KEYWORDS 🌈

option, widget, widgetadaptor, extendedclass


okay that's the whole page, bestie!! this one came from itcl version 4.0, and you can find it filed under option(3itcl) β€” go forth and define your options, you've absolutely got this πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from