hpcflow.sdk.cli.ErrorPropagatingClickContext#
- class hpcflow.sdk.cli.ErrorPropagatingClickContext(command, parent=None, info_name=None, obj=None, auto_envvar_prefix=None, default_map=None, terminal_width=None, max_content_width=None, resilient_parsing=False, allow_extra_args=None, allow_interspersed_args=None, ignore_unknown_options=None, help_option_names=None, token_normalize_func=None, color=None, show_default=None)#
Bases:
Context
A click Context class that passes on exception information.
Using the standard click.Context class, exceptions raised when using a resource specified with ctx.with_resource(my_ctx_manager()) are not passed on to the __exit__ method of my_ctx_manager. See: pallets/click#2447.
Examples
>>> @click.group() ... @click.pass_context ... def cli(ctx): ... ctx.with_resource(my_context_manager()) ... cli.context_class = ErrorPropagatingClickContext
Methods
Aborts the script.
Register a function to be called when the context tears down.
Invoke all close callbacks registered with
call_on_close()
, and exit all context managers entered withwith_resource()
.Like
find_object()
but sets the innermost object to a new instance of object_type if it does not exist.Exits the application with a given exit code.
Aborts the execution of the program with a specific error message.
Finds the closest object of a given type.
Finds the outermost context.
Similar to
invoke()
but fills in default keyword arguments from the current context if the other command expects it.Helper method to get formatted help page for the current context and command.
Get the source of a parameter.
Helper method to get formatted usage string for the current context and command.
Invokes a command callback in exactly the way it expects.
Get the default for a parameter from
default_map
.Creates the
HelpFormatter
for the help and usage output.This helper method can be used with the context object to promote it to the current thread local (see
get_current_context()
).Set the source of a parameter.
Gather information that could be useful for a tool generating user-facing documentation.
Register a resource as if it were used in a
with
statement.Attributes
The computed command path.
This is a dictionary which is shared with all the contexts that are nested.
the parent context or None if none exists.
the
Command
for this context.the descriptive information name
Map of parameter names to their parsed values.
the leftover arguments.
protected arguments.
the user object stored.
This flag indicates if a subcommand is going to be executed.
The width of the terminal (None is autodetection).
The maximum width of formatted content (None implies a sensible default which is 80 for most things).
Indicates if the context allows extra args or if it should fail on parsing.
Indicates if the context allows mixing of arguments and options or not.
Instructs click to ignore options that a command does not understand and will store it on the context for later processing.
The names for the help options.
An optional normalization function for tokens.
Indicates if resilient parsing is enabled.
Controls if styling output is wanted or not.
Show option default values when formatting help text.
- Parameters:
command (Command) –
parent (Context | None) –
info_name (str | None) –
obj (Any | None) –
auto_envvar_prefix (str | None) –
default_map (MutableMapping[str, Any] | None) –
terminal_width (int | None) –
max_content_width (int | None) –
resilient_parsing (bool) –
allow_extra_args (bool | None) –
allow_interspersed_args (bool | None) –
ignore_unknown_options (bool | None) –
color (bool | None) –
show_default (bool | None) –
- abort()#
Aborts the script.
- Return type:
te.NoReturn
- allow_extra_args#
Indicates if the context allows extra args or if it should fail on parsing.
New in version 3.0.
- allow_interspersed_args: bool#
Indicates if the context allows mixing of arguments and options or not.
New in version 3.0.
- call_on_close(f)#
Register a function to be called when the context tears down.
This can be used to close resources opened during the script execution. Resources that support Python’s context manager protocol which would be used in a
with
statement should be registered withwith_resource()
instead.
- close()#
Invoke all close callbacks registered with
call_on_close()
, and exit all context managers entered withwith_resource()
.- Return type:
None
- command#
the
Command
for this context.
- property command_path: str#
The computed command path. This is used for the
usage
information on the help page. It’s automatically created by combining the info names of the chain of contexts to the root.
- ensure_object(object_type)#
Like
find_object()
but sets the innermost object to a new instance of object_type if it does not exist.- Parameters:
object_type (Type[V]) –
- Return type:
V
- exit(code=0)#
Exits the application with a given exit code.
- Parameters:
code (int) –
- Return type:
te.NoReturn
- fail(message)#
Aborts the execution of the program with a specific error message.
- Parameters:
message (str) – the error message to fail with.
- Return type:
te.NoReturn
- find_object(object_type)#
Finds the closest object of a given type.
- Parameters:
object_type (Type[V]) –
- Return type:
V | None
- find_root()#
Finds the outermost context.
- Return type:
Context
- formatter_class#
alias of
HelpFormatter
- forward(_Context__cmd, *args, **kwargs)#
Similar to
invoke()
but fills in default keyword arguments from the current context if the other command expects it. This cannot invoke callbacks directly, only other commands.Changed in version 8.0: All
kwargs
are tracked inparams
so they will be passed ifforward
is called at multiple levels.
- get_help()#
Helper method to get formatted help page for the current context and command.
- Return type:
- get_parameter_source(name)#
Get the source of a parameter. This indicates the location from which the value of the parameter was obtained.
This can be useful for determining when a user specified a value on the command line that is the same as the default value. It will be
DEFAULT
only if the value was actually taken from the default.- Parameters:
name (str) – The name of the parameter.
- Return type:
ParameterSource
Changed in version 8.0: Returns
None
if the parameter was not provided from any source.
- get_usage()#
Helper method to get formatted usage string for the current context and command.
- Return type:
- ignore_unknown_options: bool#
Instructs click to ignore options that a command does not understand and will store it on the context for later processing. This is primarily useful for situations where you want to call into external programs. Generally this pattern is strongly discouraged because it’s not possibly to losslessly forward all arguments.
New in version 4.0.
- info_name#
the descriptive information name
- invoke(_Context__callback, *args, **kwargs)#
Invokes a command callback in exactly the way it expects. There are two ways to invoke this method:
the first argument can be a callback and all other arguments and keyword arguments are forwarded directly to the function.
the first argument is a click command object. In that case all arguments are forwarded as well but proper click parameters (options and click arguments) must be keyword arguments and Click will fill in defaults.
Note that before Click 3.2 keyword arguments were not properly filled in against the intention of this code and no context was created. For more information about this change and why it was done in a bugfix release see upgrade-to-3.2.
- invoked_subcommand: t.Optional[str]#
This flag indicates if a subcommand is going to be executed. A group callback can use this information to figure out if it’s being executed directly or because the execution flow passes onwards to a subcommand. By default it’s None, but it can be the name of the subcommand to execute.
If chaining is enabled this will be set to
'*'
in case any commands are executed. It is however not possible to figure out which ones. If you require this knowledge you should use aresult_callback()
.
- lookup_default(name, call=True)#
Get the default for a parameter from
default_map
.- Parameters:
- Return type:
Any | None
Changed in version 8.0: Added the
call
parameter.
- make_formatter()#
Creates the
HelpFormatter
for the help and usage output.To quickly customize the formatter class used without overriding this method, set the
formatter_class
attribute.Changed in version 8.0: Added the
formatter_class
attribute.- Return type:
HelpFormatter
- max_content_width: t.Optional[int]#
The maximum width of formatted content (None implies a sensible default which is 80 for most things).
- property meta: Dict[str, Any]#
This is a dictionary which is shared with all the contexts that are nested. It exists so that click utilities can store some state here if they need to. It is however the responsibility of that code to manage this dictionary well.
The keys are supposed to be unique dotted strings. For instance module paths are a good choice for it. What is stored in there is irrelevant for the operation of click. However what is important is that code that places data here adheres to the general semantics of the system.
Example usage:
LANG_KEY = f'{__name__}.lang' def set_language(value): ctx = get_current_context() ctx.meta[LANG_KEY] = value def get_language(): return get_current_context().meta.get(LANG_KEY, 'en_US')
New in version 5.0.
- obj: t.Any#
the user object stored.
- params: t.Dict[str, t.Any]#
Map of parameter names to their parsed values. Parameters with
expose_value=False
are not stored.
- parent#
the parent context or None if none exists.
- protected_args: t.List[str]#
protected arguments. These are arguments that are prepended to args when certain parsing scenarios are encountered but must be never propagated to another arguments. This is used to implement nested parsing.
- resilient_parsing: bool#
Indicates if resilient parsing is enabled. In that case Click will do its best to not cause any failures and default values will be ignored. Useful for completion.
- scope(cleanup=True)#
This helper method can be used with the context object to promote it to the current thread local (see
get_current_context()
). The default behavior of this is to invoke the cleanup functions which can be disabled by setting cleanup to False. The cleanup functions are typically used for things such as closing file handles.If the cleanup is intended the context object can also be directly used as a context manager.
Example usage:
with ctx.scope(): assert get_current_context() is ctx
This is equivalent:
with ctx: assert get_current_context() is ctx
New in version 5.0.
- Parameters:
cleanup (bool) – controls if the cleanup functions should be run or not. The default is to run these functions. In some situations the context only wants to be temporarily pushed in which case this can be disabled. Nested pushes automatically defer the cleanup.
- Return type:
Iterator[Context]
- set_parameter_source(name, source)#
Set the source of a parameter. This indicates the location from which the value of the parameter was obtained.
- Parameters:
name (str) – The name of the parameter.
source (ParameterSource) – A member of
ParameterSource
.
- Return type:
None
- to_info_dict()#
Gather information that could be useful for a tool generating user-facing documentation. This traverses the entire CLI structure.
with Context(cli) as ctx: info = ctx.to_info_dict()
New in version 8.0.
- token_normalize_func: t.Optional[t.Callable[[str], str]]#
An optional normalization function for tokens. This is options, choices, commands etc.
- with_resource(context_manager)#
Register a resource as if it were used in a
with
statement. The resource will be cleaned up when the context is popped.Uses
contextlib.ExitStack.enter_context()
. It calls the resource’s__enter__()
method and returns the result. When the context is popped, it closes the stack, which calls the resource’s__exit__()
method.To register a cleanup function for something that isn’t a context manager, use
call_on_close()
. Or use something fromcontextlib
to turn it into a context manager first.@click.group() @click.option("--name") @click.pass_context def cli(ctx): ctx.obj = ctx.with_resource(connect_db(name))
- Parameters:
context_manager (ContextManager[V, bool | None]) – The context manager to enter.
- Returns:
Whatever
context_manager.__enter__()
returns.- Return type:
V
New in version 8.0.