-
Notifications
You must be signed in to change notification settings - Fork 14
/
cli.py
executable file
·118 lines (110 loc) · 3.46 KB
/
cli.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from typing import Optional, Tuple
import click
from .config import read_config_file
from .core import reformat
from .errors import EmptySources
from .options import AlignmentMode, NewlineMode, Options, TagLineMode, WriteBackMode
from .report import Report
from .utils import out
from .version import __version__
@click.command()
@click.argument(
"src",
nargs=-1,
type=click.Path(
exists=True, file_okay=True, dir_okay=True, readable=True, resolve_path=True
),
is_eager=True,
)
@click.option(
"--check",
is_flag=True,
help=(
"Don't write the files back, just return the status. Return code 0 "
"means nothing would change. Return code 1 means some files would be "
"reformatted. Return code 123 means there was an internal error."
),
)
@click.option(
"-a",
"--alignment",
type=click.Choice([AlignmentMode.LEFT.value, AlignmentMode.RIGHT.value]),
help=(
"Specify the alignment of step keywords (Given, When, Then,...). "
"If specified, all statements after step keywords are left-aligned, "
"spaces are inserted before/after the keywords to right/left align them. "
"By default, step keywords are left-aligned, and there is a single "
"space between the step keyword and the statement."
),
)
@click.option(
"-n",
"--newline",
type=click.Choice([NewlineMode.LF.value, NewlineMode.CRLF.value]),
help=(
"Specify the line separators when formatting files inplace. "
"If not specified, line separators are preserved."
),
)
@click.option(
"--fast/--safe",
is_flag=True,
help="If --fast given, skip the sanity checks of file contents. [default: --safe]",
)
@click.option(
"--single-line-tags/--multi-line-tags",
is_flag=True,
default=False,
help=(
"If --single-line-tags given, output consecutive tags on one line. "
"If --multi-line-tags given, output one tag per line. "
"[default: --multi-line-tags]"
),
)
@click.option(
"--config",
type=click.Path(
exists=True, file_okay=True, dir_okay=False, readable=True, allow_dash=False
),
is_eager=True,
callback=read_config_file,
help="Read configuration from FILE.",
)
@click.version_option(version=__version__)
@click.pass_context
def main(
ctx: click.Context,
src: Tuple[str],
check: bool,
alignment: Optional[str],
newline: Optional[str],
fast: bool,
single_line_tags: bool,
config: Optional[str],
) -> None:
"""
Reformat the given Gherkin files and all files in the given directories recursively.
"""
if config:
out(f"Using configuration from {config}.", bold=False, fg="blue")
write_back_mode = WriteBackMode.from_configuration(check)
alignment_mode = AlignmentMode.from_configuration(alignment)
newline_mode = NewlineMode.from_configuration(newline)
tag_line_mode = TagLineMode.from_configuration(single_line_tags)
options = Options(
write_back=write_back_mode,
step_keyword_alignment=alignment_mode,
newline=newline_mode,
fast=fast,
tag_line_mode=tag_line_mode,
)
report = Report(check=check)
try:
reformat(src, report, options=options)
except EmptySources:
out("No paths given. Nothing to do 😴")
ctx.exit(0)
bang = "💥 💔 💥" if report.return_code else "✨ 🍰 ✨"
out(f"All done! {bang}")
click.secho(str(report), err=True)
ctx.exit(report.return_code)