6.1.3 Using Multiple Contexts

Did you also notice that after creating a context, there is actually never any explicit reference to it anymore? So again, where is the magick? In fact, there’s no magick at all involved here either.

Clon has a global variable named *context* which holds the current context. When you create a context with make-context, it is automatically made current, unless you use the :make-current initarg with a value of nil.

The whole runtime phase API of Clon uses a context implicitely. This involves progname, remainder, cmdline-options-p, cmdline-p, getopt, getopt-cmdline, multiple-value-getopt-cmdline, do-cmdline-options and help. As a consequence, it is possible to use Clon with multiple contexts at the same time. There are in fact three ways to achieve this.

  1. At any time in your program, you may change the value of *context*. All subsequent calls to the runtime phase API will hence use this other context.
  2. Clon also provides a macro which changes the current context for you.
    Function: with-context CONTEXT &body BODY

    Execute BODY with *context* bound to CONTEXT.

  3. If you prefer to use another context only once, you can use the :context key instead. The whole runtime phase API of Clon understands it. For the functions getopt, getopt-cmdline and help, it’s just another key in addition to those we’ve already seen. For the macros multiple-value-getopt-cmdline and do-cmdline-options, the key must appear at the end of the first (list) argument, like this:
    (multiple-value-getopt-cmdline (option name value :context ctx) ...)
    (do-cmdline-options (option name value :context ctx) ...)