-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcli.py
More file actions
59 lines (42 loc) · 1.54 KB
/
cli.py
File metadata and controls
59 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Implements the command line interface."""
from __future__ import annotations
import importlib.metadata
from typing import Any
import click
from _pytask.click import ColoredGroup
from _pytask.pluginmanager import storage
_CONTEXT_SETTINGS: dict[str, Any] = {
"help_option_names": ("-h", "--help"),
"show_default": True,
}
if importlib.metadata.version("click") >= "8": # pragma: no cover
_VERSION_OPTION_KWARGS = {"package_name": "pytask"}
else: # pragma: no cover
_VERSION_OPTION_KWARGS = {}
def _extend_command_line_interface(cli: click.Group) -> click.Group:
"""Add parameters from plugins to the commandline interface."""
pm = storage.create()
pm.hook.pytask_extend_command_line_interface.call_historic(kwargs={"cli": cli})
_sort_options_for_each_command_alphabetically(cli)
return cli
def _sort_options_for_each_command_alphabetically(cli: click.Group) -> None:
"""Sort command line options and arguments for each command alphabetically."""
for command in cli.commands:
cli.commands[command].params = sorted(
cli.commands[command].params, key=lambda x: x.opts[0].replace("-", "")
)
@click.group(
cls=ColoredGroup,
context_settings=_CONTEXT_SETTINGS,
default="build",
default_if_no_args=True,
)
@click.version_option(**_VERSION_OPTION_KWARGS)
def cli() -> None:
"""Manage your tasks with pytask."""
_extend_command_line_interface(cli)
DEFAULTS_FROM_CLI = {
option.name: option.default
for command in cli.commands.values()
for option in command.params
}