class documentation

class BaseCommand:

Known subclasses: click.core.Command

View In Hierarchy

The base command implements the minimal API contract of commands. Most code will never use this as it does not implement a lot of useful functionality but it can act as the direct subclass of alternative parsing methods that do not depend on the Click parser.

For instance, this can be used to bridge Click and other systems like argparse or docopt.

Because base commands do not implement a lot of the API that other parts of Click take for granted, they are not supported for all operations. For instance, they cannot be used with the decorators usually and they have no built-in callback system.

Changed in version 2.0: Added the context_settings parameter.
Parameters
namethe name of the command to use unless a group overrides it.
context​_settingsan optional dictionary with defaults that are passed to the context object.
Method __call__ Alias for main.
Method __init__ Undocumented
Method __repr__ Undocumented
Method ​_main​_shell​_completion Check if the shell is asking for tab completion, process that, then exit early. Called from main before the program is invoked.
Method get​_help Undocumented
Method get​_usage Undocumented
Method invoke Given a context, this invokes the command. The default implementation is raising a not implemented error.
Method main No summary
Method make​_context This function when given an info name and arguments will kick off the parsing and create a new Context. It does not invoke the actual command callback though.
Method parse​_args Given a context and a list of arguments this creates the parser and parses the arguments, then modifies the context as necessary. This is automatically invoked by make_context.
Method shell​_complete Return a list of completions for the incomplete value. Looks at the names of chained multi-commands.
Method to​_info​_dict Gather information that could be useful for a tool generating user-facing documentation. This traverses the entire structure below this command.
Class Variable allow​_extra​_args Undocumented
Class Variable allow​_interspersed​_args Undocumented
Class Variable ignore​_unknown​_options Undocumented
Instance Variable context​_settings Undocumented
Instance Variable name Undocumented
def __call__(self, *args, **kwargs):
Alias for main.
Parameters
*args:t.AnyUndocumented
**kwargs:t.AnyUndocumented
Returns
t.AnyUndocumented
def __init__(self, name, context_settings=None):
overridden in click.core.Command

Undocumented

Parameters
name:t.Optional[str]Undocumented
context​_settings:t.Optional[t.Dict[str, t.Any]]Undocumented
def __repr__(self):

Undocumented

Returns
strUndocumented
def _main_shell_completion(self, ctx_args, prog_name, complete_var=None):
Check if the shell is asking for tab completion, process that, then exit early. Called from main before the program is invoked.
Parameters
ctx​_args:t.Dict[str, t.Any]Undocumented
prog​_name:strName of the executable in the shell.
complete​_var:t.Optional[str]Name of the environment variable that holds the completion instruction. Defaults to _{PROG_NAME}_COMPLETE.
def get_help(self, ctx):
overridden in click.core.Command

Undocumented

Parameters
ctx:ContextUndocumented
Returns
strUndocumented
def get_usage(self, ctx):
overridden in click.core.Command

Undocumented

Parameters
ctx:ContextUndocumented
Returns
strUndocumented
def invoke(self, ctx):
overridden in click.core.Command
Given a context, this invokes the command. The default implementation is raising a not implemented error.
Parameters
ctx:ContextUndocumented
Returns
t.AnyUndocumented
def main(self, args=None, prog_name=None, complete_var=None, standalone_mode=True, windows_expand_args=True, **extra):

This is the way to invoke a script with all the bells and whistles as a command line application. This will always terminate the application after a call. If this is not wanted, SystemExit needs to be caught.

This method is also available by directly calling the instance of a Command.

Changed in version 8.0.1: Added the windows_expand_args parameter to allow disabling command line arg expansion on Windows.
Changed in version 8.0: When taking arguments from sys.argv on Windows, glob patterns, user dir, and env vars are expanded.
Changed in version 3.0: Added the standalone_mode parameter.
Parameters
args:t.Optional[t.Sequence[str]]the arguments that should be used for parsing. If not provided, sys.argv[1:] is used.
prog​_name:t.Optional[str]the program name that should be used. By default the program name is constructed by taking the file name from sys.argv[0].
complete​_var:t.Optional[str]the environment variable that controls the bash completion support. The default is "_<prog_name>_COMPLETE" with prog_name in uppercase.
standalone​_mode:boolthe default behavior is to invoke the script in standalone mode. Click will then handle exceptions and convert them into error messages and the function will never return but shut down the interpreter. If this is set to False they will be propagated to the caller and the return value of this function is the return value of invoke.
windows​_expand​_args:boolExpand glob patterns, user dir, and env vars in command line args on Windows.
**extra:t.Anyextra keyword arguments are forwarded to the context constructor. See Context for more information.
Returns
t.AnyUndocumented
def make_context(self, info_name, args, parent=None, **extra):

This function when given an info name and arguments will kick off the parsing and create a new Context. It does not invoke the actual command callback though.

To quickly customize the context class used without overriding this method, set the context_class attribute.

Changed in version 8.0: Added the context_class attribute.
Parameters
info​_name:t.Optional[str]the info name for this invocation. Generally this is the most descriptive name for the script or command. For the toplevel script it's usually the name of the script, for commands below it it's the name of the command.
args:t.List[str]the arguments to parse as list of strings.
parent:t.Optional[Context]the parent context if available.
**extra:t.Anyextra keyword arguments forwarded to the context constructor.
Returns
ContextUndocumented
def parse_args(self, ctx, args):
overridden in click.core.Command
Given a context and a list of arguments this creates the parser and parses the arguments, then modifies the context as necessary. This is automatically invoked by make_context.
Parameters
ctx:ContextUndocumented
args:t.List[str]Undocumented
Returns
t.List[str]Undocumented
def shell_complete(self, ctx, incomplete):
overridden in click.core.Command

Return a list of completions for the incomplete value. Looks at the names of chained multi-commands.

Any command could be part of a chained multi-command, so sibling commands are valid at any point during command completion. Other command classes will return more completions.

New in version 8.0.
Parameters
ctx:ContextInvocation context for this command.
incomplete:strValue being completed. May be empty.
Returns
t.List[CompletionItem]Undocumented
def to_info_dict(self, ctx):
overridden in click.core.Command

Gather information that could be useful for a tool generating user-facing documentation. This traverses the entire structure below this command.

Use click.Context.to_info_dict to traverse the entire CLI structure.

New in version 8.0.
Parameters
ctx:ContextA Context representing this command.
Returns
t.Dict[str, t.Any]Undocumented
allow_extra_args: bool =

Undocumented

allow_interspersed_args: bool =

Undocumented

ignore_unknown_options: bool =

Undocumented

context_settings: t.Dict[str, t.Any] =

Undocumented

name =

Undocumented