Skip to content
Myr

Changed wording with respect to context objects

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kevin Yap · committed by Kevin Yap · parent 0f894fa · Browse files · 2 files+10 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+10 added−10 removed

  • other:+10 added−10 removed

2 files+10 added−10 removed

modified:docs/complex.rst+9 added-9 removed
30 unmodified lines
31at the end of the function, and so forth. It can also optionally hold an32application-defined object.3334Context objects build a linked list until they hit the top one. Each context35is linked to the parent context. This allows a command to work below35is linked to a parent context. This allows a command to work below36another command and store its own information there without having to be37afraid of altering up the state of the parent command.3839Because the parent data is available, however, it is possible to navigate to
6 unmodified lines
46``````````````````4748When a click command callback is executed, it's passed all the non-hidden49parameters as keyword arguments. Notably absent is the context. However,50a callback can opt into being passed the context object by marking itself50a callback can opt into being passed to the context object by marking itself51with :func:`pass_context`.5253So how do you invoke a command callback if you don't know if it should54receive the context or not? The answer is that the context itself
45 unmodified lines
100``Repo`` class. This holds the state for our command line tool. In this101case, it just remembers some parameters, but at this point it could also102start loading configuration files and so on.103104This state object is then remembered as :attr:`~Context.obj` on the105context. This is a special attribute where commands are supposed to106remember what they need to pass on to their children.104This state object is then remembered by the context as :attr:`~Context.obj`.105This is a special attribute where commands are supposed to remember what106they need to pass on to their children.107108In order for this to work, we need to mark our function with109:func:`pass_context`, because otherwise the context object would be109:func:`pass_context`, because otherwise, the context object would be110entirely hidden from us.111112The First Child Command113```````````````````````
41 unmodified lines
155156There is a much better system that can built by taking advantage of the linked157nature of contexts. We know that the plugin context is linked to the context158that created our repo. Because of that, we can start a search for the last159level where the object stored on the context was a repo.159level where the object stored by the context was a repo.160161Built-in support for this is provided by the :func:`make_pass_decorator`162factory, which will create decorators for us that find objects (it163internally calls into :meth:`Context.find_object`). In our case, we
19 unmodified lines
183Ensuring Object Creation184````````````````````````185186The above example only works if there was an outer command that created a187``Repo`` object and stored it on the context. For some more advanced use187``Repo`` object and stored it in the context. For some more advanced use188cases, this might become a problem. The default behavior of189:func:`make_pass_decorator` is to call :meth:`Context.find_object`190which will find the object. If it can't find the object, it will raise an191error. The alternative behavior is to use :meth:`Context.ensure_object`192which will find the object, and if it cannot find it, will create one and193store it on the innermost context. This behavior can also be enabled for193store it in the innermost context. This behavior can also be enabled for194:func:`make_pass_decorator` by passing ``ensure=True``:195196.. click:example::197
22 unmodified lines
modified:docs/options.rst+1 added-1 removed
329 unmodified lines
330331The `expose_value` parameter prevents the pretty pointless ``version``332parameter from being passed to the callback. If that was not specified, a333boolean would be passed to the `hello` script. The `resilient_parsing`334flag is set on the context if click wants to parse the command line334flag is applied to the context if click wants to parse the command line335without and destructive behavior that would change the execution flow. In336this case, because we would exit the program, we instead do nothing.337338What it looks like:
270 unmodified lines