-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.py
81 lines (64 loc) · 2.51 KB
/
main.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#
# Copyright (c) 2020-2021 Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
"""Main cli entry point."""
import logging
import sys
from pkg_resources import get_distribution
from typing import Union, Any
import click
from mbed_tools.lib.logging import set_log_level, MbedToolsHandler
from mbed_tools.cli.configure import configure
from mbed_tools.cli.list_connected_devices import list_connected_devices
from mbed_tools.cli.project_management import new, import_, deploy
from mbed_tools.cli.build import build
from mbed_tools.cli.sterm import sterm
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
LOGGER = logging.getLogger(__name__)
class GroupWithExceptionHandling(click.Group):
"""A click.Group which handles ToolsErrors and logging."""
def invoke(self, context: click.Context) -> None:
"""Invoke the command group.
Args:
context: The current click context.
"""
# Use the context manager to ensure tools exceptions (expected behaviour) are shown as messages to the user,
# but all other exceptions (unexpected behaviour) are shown as errors.
with MbedToolsHandler(LOGGER, context.params["traceback"]) as handler:
super().invoke(context)
sys.exit(handler.exit_code)
def print_version(context: click.Context, param: Union[click.Option, click.Parameter], value: bool) -> Any:
"""Print the version of mbed-tools."""
if not value or context.resilient_parsing:
return
version_string = get_distribution("mbed-tools").version
click.echo(version_string)
context.exit()
@click.group(cls=GroupWithExceptionHandling, context_settings=CONTEXT_SETTINGS)
@click.option(
"--version",
is_flag=True,
callback=print_version,
expose_value=False,
is_eager=True,
help="Display versions of all Mbed Tools packages.",
)
@click.option(
"-v",
"--verbose",
default=0,
count=True,
help="Set the verbosity level, enter multiple times to increase verbosity.",
)
@click.option("-t", "--traceback", is_flag=True, show_default=True, help="Show a traceback when an error is raised.")
def cli(verbose: int, traceback: bool) -> None:
"""Command line tool for interacting with Mbed OS."""
set_log_level(verbose)
cli.add_command(configure, "configure")
cli.add_command(list_connected_devices, "detect")
cli.add_command(new, "new")
cli.add_command(deploy, "deploy")
cli.add_command(import_, "import")
cli.add_command(build, "compile")
cli.add_command(sterm, "sterm")