Skip to content

Commit b463a53

Browse files
committed
Refactor some CLI code (mainly inlining options)
Some click options are only used once, and some need different help strings depending on which subcommand they apply to, so most of the option definitions have now been inlined.
1 parent e6eab24 commit b463a53

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

xcengine/cli.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,11 @@ def cli(verbose):
2626
logging.getLogger().setLevel(logging.DEBUG)
2727

2828

29-
batch_option = click.option(
30-
"-b", "--batch", is_flag=True, help="Run as batch script after creating"
31-
)
32-
33-
output_option = click.option(
34-
"-o",
35-
"--output",
36-
type=click.Path(path_type=pathlib.Path, dir_okay=True, file_okay=False),
37-
help="Write output data to this directory, which will be created if it "
38-
"does not exist already.",
39-
)
40-
4129
from_saved_option = click.option(
4230
"-f",
4331
"--from-saved",
4432
is_flag=True,
45-
help="If batch and server both used, serve datasets from saved Zarrs",
46-
)
47-
48-
keep_option = click.option(
49-
"-k",
50-
"--keep",
51-
is_flag=True,
52-
help="Keep container after it has finished running.",
33+
help="If --batch and --server both used, serve datasets from saved Zarrs",
5334
)
5435

5536
notebook_argument = click.argument(
@@ -59,17 +40,22 @@ def cli(verbose):
5940
),
6041
)
6142

62-
server_option = click.option(
43+
44+
@cli.command(
45+
help="Create a compute engine script on the host system. "
46+
"The output directory will be used for the generated "
47+
"script, supporting code modules, and any output "
48+
"produced by running the script."
49+
)
50+
@click.option(
51+
"-b", "--batch", is_flag=True, help="Run as batch script after creating"
52+
)
53+
@click.option(
6354
"-s",
6455
"--server",
6556
is_flag=True,
6657
help="Run the script as an xcube server after creating it.",
6758
)
68-
69-
70-
@cli.command(help="Create a compute engine script on the host system")
71-
@batch_option
72-
@server_option
7359
@from_saved_option
7460
@click.option(
7561
"-c",
@@ -133,7 +119,7 @@ def image_cli():
133119
type=str,
134120
default=None,
135121
help="Tag to apply to the Docker image. "
136-
"If not specified, a timestamp-based tag will be generated automatically",
122+
"If not specified, a timestamp-based tag will be generated automatically.",
137123
)
138124
@click.option(
139125
"-a",
@@ -168,8 +154,18 @@ def build(
168154

169155

170156
@image_cli.command(help="Run a compute engine image as a Docker container")
171-
@batch_option
172-
@server_option
157+
@click.option(
158+
"-b",
159+
"--batch",
160+
is_flag=True,
161+
help="Run the compute engine as a batch script",
162+
)
163+
@click.option(
164+
"-s",
165+
"--server",
166+
is_flag=True,
167+
help="Run the compute engine as an xcube server.",
168+
)
173169
@click.option(
174170
"-p",
175171
"--port",
@@ -179,8 +175,19 @@ def build(
179175
help="Host port for xcube server (default: 8080). Implies --server.",
180176
)
181177
@from_saved_option
182-
@output_option
183-
@keep_option
178+
@click.option(
179+
"-o",
180+
"--output",
181+
type=click.Path(path_type=pathlib.Path, dir_okay=True, file_okay=False),
182+
help="Write output data to this directory, which will be created if it "
183+
"does not exist already.",
184+
)
185+
@click.option(
186+
"-k",
187+
"--keep",
188+
is_flag=True,
189+
help="Keep container after it has finished running.",
190+
)
184191
@click.argument("image", type=str)
185192
@click.pass_context
186193
def run(

xcengine/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ def __init__(
163163
self.environment = environment
164164
self.build_dir = build_dir
165165
if tag is None:
166-
self.tag = (
167-
datetime.datetime.now(datetime.UTC).strftime(self.tag_format)
166+
self.tag = datetime.datetime.now(datetime.UTC).strftime(
167+
self.tag_format
168168
)
169169
LOGGER.info(f"No tag specified; using {self.tag}")
170170
else:

0 commit comments

Comments
 (0)