clapper.click¶
Helpers to build command-line interfaces (CLI) via click
.
Module Attributes
Module logger. |
Functions
|
Add a command group to list/describe/copy job configurations. |
|
Log the click parameters with the logging module. |
|
Add a command group to read/write RC configuration. |
|
Click-option decorator that adds a |
Classes
|
Class that handles prefix aliasing for commands. |
|
A |
Custom parameter class allowing click to receive complex Python types as parameters. |
|
|
An extended |
- clapper.click.module_logger = <Logger clapper.click (WARNING)>¶
Module logger.
- clapper.click.verbosity_option(logger, short_name='v', name='verbose', dflt=0, **kwargs)[source]¶
Click-option decorator that adds a
-v
/--verbose
option to a cli.This decorator adds a click option to your CLI to set the log-level on a provided
logging.Logger
. You must specifically determine the logger that will be affected by this CLI option, via thelogger
option.@verbosity_option(logger=logger)
The verbosity option has the “count” type, and has a default value of 0. At each time you provide
-v
options on the command-line, this value is increased by one. For example, a CLI setting of-vvv
will set the value of this option to 3. This is the mapping between the value of this option (count of-v
CLI options passed) and the log-level set at the provided logger:0 (no
-v
option provided):logger.setLevel(logging.ERROR)
1 (
-v
):logger.setLevel(logging.WARNING)
2 (
-vv
):logger.setLevel(logging.INFO)
3 (
-vvv
or more):logger.setLevel(logging.DEBUG)
The verbosity level specified in this option will also be set during the loading of the
ResourceOption
orCommandConfig
configuration files unlessverbosity_option
is aResourceOption
itself. If this is the case, the logging level during the configuration loading will be the default level of the logger and the option will only effect the logging after the options handling.- Parameters:
logger (
Logger
) – Thelogging.Logger
to be set.short_name (
str
) – Short name of the option. If not set, then usev
name (
str
) – Long name of the option. If not set, then useverbose
– this will also become the name of the contextual parameter for click.dflt (
int
) – The default verbosity level to use (defaults to 0).**kwargs (
Any
) – Further keyword-arguments to be forwarded to the underlyingclick.option()
- Return type:
- Returns:
A callable, that follows the
click
-framework policy for option decorators. Use it accordingly.
- class clapper.click.ConfigCommand(name, *args, help=None, entry_point_group=None, **kwargs)[source]¶
Bases:
Command
A
click.Command
that can read options from config files.Warning
In order to use this class, you have to use the
ResourceOption
class also.- Parameters:
name (
str
) – The name to be used for the configuration argument*args (
tuple
) – Unnamed parameters passed toclick.Command
help (
str
|None
) – Help message associated with this commandentry_point_group (
str
|None
) – Name of the entry point group from which entry-points will be searched**kwargs (
Any
) – Named parameters passed toclick.Command
- class clapper.click.CustomParamType[source]¶
Bases:
ParamType
Custom parameter class allowing click to receive complex Python types as parameters.
- name: str = 'custom'¶
the descriptive name of this type
- class clapper.click.ResourceOption(param_decls=None, show_default=False, prompt=False, confirmation_prompt=False, hide_input=False, is_flag=None, flag_value=None, multiple=False, count=False, allow_from_autoenv=True, type=None, help=None, entry_point_group=None, required=False, string_exceptions=None, **kwargs)[source]¶
Bases:
Option
An extended
click.Option
that automatically loads resources from config files.This class comes with two different functionalities that are independent and could be combined:
If used in commands that are inherited from
ConfigCommand
, it will lookup inside the config files (that are provided as argument to the command) to resolve its value. Values given explicitly in the command line take precedence.If
entry_point_group
is provided, it will treat values given to it (by any means) as resources to be loaded. Loading is done usingconfig.load()
. Check Loading Single Objects for more details on this topic. The final value cannot be a string.
You may use this class in three ways:
Using this class (without using
ConfigCommand
) AND (providingentry_point_group
).Using this class (with
ConfigCommand
) AND (providing entry_point_group).Using this class (with
ConfigCommand
) AND (without providing entry_point_group).
Using this class without
ConfigCommand
and without providing entry_point_group does nothing and is not allowed.-
entry_point_group:
str
|None
¶ If provided, the strings values to this option are assumed to be entry points from
entry_point_group
that need to be loaded.This may be different than the wrapping
ConfigCommand
.
-
string_exceptions:
list
[str
] |None
¶ If provided and
entry_point_group
is provided, the code will not treat strings instring_exceptions
as entry points and does not try to load them.
- consume_value(ctx, opts)[source]¶
Retrieve value for parameter from appropriate context.
This method will retrieve the value of its own parameter from the appropriate context, by trying various sources.
- Parameters:
- Return type:
- Returns:
A tuple containing the parameter value (of any type) and the source it used to retrieve it.
- class clapper.click.AliasedGroup(name=None, commands=None, invoke_without_command=False, no_args_is_help=None, subcommand_metavar=None, chain=False, result_callback=None, **kwargs)[source]¶
Bases:
Group
Class that handles prefix aliasing for commands.
Basically just implements get_command that is used by click to choose the command based on the name.
Example
To enable prefix aliasing of commands for a given group, just set
cls=AliasedGroup
parameter in click.group decorator.
- clapper.click.user_defaults_group(logger, config)[source]¶
Add a command group to read/write RC configuration.
This decorator adds a whole command group to a user predefined function which is part of the user’s CLI. The command group allows the user to get and set options through the command-line interface:
import logging from expose.rc import UserDefaults from expose.click import user_defaults_group logger = logging.getLogger(__name__) user_defaults = UserDefaults("~/.myapprc") ... @user_defaults_group(logger=logger, config=user_defaults) def rc(**kwargs): '''Use this command to affect the global user configuration.''' pass
Then use it like this:
$ user-cli rc --help usage: ...
- clapper.click.config_group(logger, entry_point_group)[source]¶
Add a command group to list/describe/copy job configurations.
This decorator adds a whole command group to a user predefined function which is part of the user’s CLI. The command group provides an interface to list, fully describe or locally copy configuration files distributed with the package. Commands accept both entry-point or module names to be provided as input.
import logging from expose.click import config_group logger = logging.getLogger(__name__) ... @config_group(logger=logger, entry_point_group="mypackage.config") def config(**kwargs): '''Use this command to list/describe/copy config files.''' pass
Then use it like this:
$ user-cli config --help usage: ...