Skip to content

Commit 53eac76

Browse files
committed
setup CLI
1 parent f0eabeb commit 53eac76

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ homepage = "https://github.com/iterorganization/Waveform-Editor"
5454
where = ["."]
5555
include = ["waveform_editor*"]
5656

57+
[project.scripts]
58+
waveform-editor = "waveform_editor.cli:cli"
59+
5760
[tool.setuptools_scm]
5861
# version_file = "waveform_editor/_version.py"
5962

waveform_editor/cli.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import logging
2+
import sys
3+
4+
import click
5+
from rich import box, console, traceback
6+
from rich.table import Table
7+
8+
import waveform_editor
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
def _excepthook(type_, value, tb):
14+
logger.debug("Suppressed traceback:", exc_info=(type_, value, tb))
15+
# Only display the last traceback frame:
16+
if tb is not None:
17+
while tb.tb_next:
18+
tb = tb.tb_next
19+
rich_tb = traceback.Traceback.from_exception(type_, value, tb, extra_lines=0)
20+
console.Console(stderr=True).print(rich_tb)
21+
22+
23+
@click.group("waveform-editor", invoke_without_command=True, no_args_is_help=True)
24+
@click.option("-v", "--version", is_flag=True, help="Show version information")
25+
def cli(version):
26+
"""The Waveform Editor command line interface.
27+
28+
Please use one of the available commands listed below. You can get help for each
29+
command by executing:
30+
31+
waveform-editor <command> --help
32+
"""
33+
# Limit the traceback to 1 item: avoid scaring CLI users with long traceback prints
34+
# and let them focus on the actual error message
35+
sys.excepthook = _excepthook
36+
37+
if version:
38+
print_version()
39+
40+
41+
def print_version():
42+
"""Print version information of the waveform editor."""
43+
cons = console.Console()
44+
grid = Table(
45+
title="waveform editor version info", show_header=False, title_style="bold"
46+
)
47+
grid.box = box.HORIZONTALS
48+
if cons.size.width > 120:
49+
grid.width = 120
50+
grid.add_row("waveform editor version:", waveform_editor.__version__)
51+
grid.add_section()
52+
console.Console().print(grid)
53+
54+
55+
if __name__ == "__main__":
56+
cli()

0 commit comments

Comments
 (0)