find_object will search object in parent context by type. If you want to use multiple objects the easiest way is to make obj a dictionary. Here example:
@click.group
@click.option('-c', 'config')
@click.pass_context
def cli(ctx, config):
if ctx.obj is None:
ctx.obj = dict()
# add any data to obj here...
ctx.obj['a'] = 'example_A'
ctx.obj['b'] = 'example_B'
@cli.command()
@click.pass_context()
def cmd_a(ctx):
# check prepared data
print(ctx.obj['a'])
@cli.command()
@cli.pass_context()
def cmd_b(ctx):
# check prepared data
print(ctx.obj['b'])
Slightly different way - use custom class for obj:
class Config(object):
def __init__(self, config):
# do something with config here ...
self.a = 'example_A'
self.b = 'example_B'
# in your def cli()
ctx.obj = Config(config)
# in your commands you can works with your prepared data like this:
print(ctx.obj.a)
print(ctx.obj.b)
Hope this helps.
Answer from Danila Ganchar on Stack OverflowClick Documentation
click.palletsprojects.com › en › stable › complex
Complex Applications — Click Documentation (8.5.x)
Whenever a Click command is executed, a Context object is created which holds state for this particular invocation. It remembers parsed parameters, what command created it, which resources need to be cleaned up at the end of the function, and so forth.
How to put context in custom object?
I have a nested application like the complex cli example, where a custom object is passed through a custom decorator composed with click.make_pass_decorator and ensure=True. If I put @pass_context ... More on github.com
command line interface - How to access the main Context object of a Python Click application - Stack Overflow
(*) I know that I can generate a new Context object via click.Context(hello) but this context does not contain by itself the info_name property. Of course I can figure out the script name manually, but Click certainly creates a default context with the script name and I believe that one would ... More on stackoverflow.com
Make `click.Context` generic for `obj`
To add some type safety to the obj attribute of the Context class, I was thinking maybe it could be made generic with respect to obj attribute. It could then be used this way (with a TypedDict but it could be any user defined object): cl... More on github.com
command line interface - Why doesn't python-click pass context between subcommands? - Stack Overflow
Click Version: 8.1.3 Python Version: 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] ----------- > fn1 fn2 obj: {'fn1': 1} ... Sign up to request clarification or add additional context in comments. More on stackoverflow.com
Readthedocs
pocoo-click.readthedocs.io › en › latest › api
API - the Click Documentation
If the cleanup is intended the context object can also be directly used as a context manager. ... New in version 5.0. ... The width of the terminal (None is autodetection). ... An optional normalization function for tokens. This is options, choices, commands etc. ... Returns the current click context.
ProgramCreek
programcreek.com › python › example › 88034 › click.Context
Python Examples of click.Context
:param click_context: click context object. :param agents: agents names. :param multithreaded: bool flag to run as multithreads. :return: None.
Readthedocs
pocoo-click.readthedocs.io › en › latest › advanced
Advanced Patterns - the Click Documentation
Starting with Click 5.0 it is possible to access the current context from anywhere within the same thread through the use of the get_current_context() function which returns it. This is primarily useful for accessing the context bound object as well as some flags that are stored on it to customize the runtime behavior.
GitHub
github.com › pallets › click › issues › 2493
Make `click.Context` generic for `obj` · Issue #2493 · pallets/click
April 16, 2023 - Make click.Context generic for obj#2493 · Copy link · Labels · typingType annotations and stubsType annotations and stubs · Viicos · opened · on Apr 16, 2023 · Issue body actions · To add some type safety to the obj attribute of the Context class, I was thinking maybe it could be made generic with respect to obj attribute. It could then be used this way (with a TypedDict but it could be any user defined object): class ContextObj(TypedDict): attr: int def subcommand( ctx: click.Context[ContextObj], ...
Author: pallets
Click Documentation
click.palletsprojects.com › en › stable › commands
Advanced Groups and Context — Click Documentation (8.5.x)
If the object is provided, each context will pass the object onwards to its children, but at any level a context’s object can be overridden.
Click Documentation
click.palletsprojects.com › api
API — Click Documentation (8.5.x)
Context manager. Yields a writable file-like object which can be used as an output pager. ... Added in version 8.4.0. ... color (bool | None) – controls if the pager supports ANSI colors or not. The default is autodetection. ... click.prompt(text: str, default: str | None = None, hide_input: ...
Click Documentation
click.palletsprojects.com › en › stable › advanced
Advanced Patterns — Click Documentation (8.5.x)
One common way to prevent a parameter from being passed to the callback is the expose_value argument to a parameter which hides the parameter entirely. The way this works is that the Context object has a params attribute which is a dictionary of all parameters.
GitHub
github.com › pallets › click › issues › 200
Setting context object inside function doesn't pass the new ...
August 12, 2014 - @click.group() @click.option('--debug/--no-debug', default=False) @click.pass_context def cli(ctx, debug): if ctx.obj is None: # Doesn't work as of 3.0 ctx.obj = {} ctx.obj['DEBUG'] = debug @cli.command() @click.pass_context def sync(ctx): click.echo('Debug is %s' % (ctx.obj['DEBUG'] and 'on' or 'off')) if __name__ == '__main__': cli()
Author: pallets
GitHub
github.com › pallets › click › issues › 516
Accessing global context in tests · Issue #516 · pallets/click
February 5, 2016 - Is there a way for me to mock this context object in a nice way. The only approach I can think of is to patch the pass_context director before I import my CLI module into my testing module. ... @click.group() @click.option('-c', '--config', default=os.path.expanduser("~/.configfile"), help='Path to configuration file') @click.pass_context def main(ctx, config): ctx.obj = { 'config': config, 'connections': { 'redis': RedisConnection(config) } } @main.command() @click.pass_context def command(ctx): conn = ctx.obj['connections'] conn['redis'].do_thing() # here i'd like to make the RedisConnection a mock during testing
Author: pallets