Skip to content

Commit 47f7cc7

Browse files
Use click choices in CLI (#493)
1 parent a009d1d commit 47f7cc7

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

janus_core/cli/eos.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing import Annotated, get_args
66

7+
from click import Choice
78
from typer import Context, Option, Typer
89
from typer_config import use_config
910

@@ -21,6 +22,7 @@
2122
WriteKwargs,
2223
)
2324
from janus_core.cli.utils import yaml_converter_callback
25+
from janus_core.helpers.janus_types import EoSNames
2426

2527
app = Typer()
2628

@@ -35,7 +37,11 @@ def eos(
3537
max_volume: Annotated[float, Option(help="Maximum volume scale factor.")] = 1.05,
3638
n_volumes: Annotated[int, Option(help="Number of volumes.")] = 7,
3739
eos_type: Annotated[
38-
str, Option(help="Type of fit for equation of state.")
40+
str,
41+
Option(
42+
click_type=Choice(get_args(EoSNames)),
43+
help="Type of fit for equation of state.",
44+
),
3945
] = "birchmurnaghan",
4046
minimize: Annotated[
4147
bool, Option(help="Whether to minimize initial structure before calculations.")
@@ -140,7 +146,6 @@ def eos(
140146
set_read_kwargs_index,
141147
start_summary,
142148
)
143-
from janus_core.helpers.janus_types import EoSNames
144149

145150
# Check options from configuration file are all valid
146151
check_config(ctx)
@@ -149,9 +154,6 @@ def eos(
149154
[read_kwargs, calc_kwargs, minimize_kwargs, write_kwargs]
150155
)
151156

152-
if eos_type not in get_args(EoSNames):
153-
raise ValueError(f"Fit type must be one of: {get_args(EoSNames)}")
154-
155157
# Set initial config
156158
all_kwargs = {
157159
"read_kwargs": read_kwargs.copy(),

janus_core/cli/md.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
from typing import Annotated, get_args
88

9+
from click import Choice
910
from typer import Context, Option, Typer
1011
from typer_config import use_config
1112
import yaml
@@ -27,6 +28,7 @@
2728
WriteKwargs,
2829
)
2930
from janus_core.cli.utils import parse_correlation_kwargs, yaml_converter_callback
31+
from janus_core.helpers.janus_types import Ensembles
3032

3133
app = Typer()
3234

@@ -58,7 +60,13 @@ def _update_restart_files(summary: Path, restart_files: list[Path]) -> None:
5860
def md(
5961
# numpydoc ignore=PR02
6062
ctx: Context,
61-
ensemble: Annotated[str, Option(help="Name of thermodynamic ensemble.")],
63+
ensemble: Annotated[
64+
str,
65+
Option(
66+
click_type=Choice(get_args(Ensembles)),
67+
help="Name of thermodynamic ensemble.",
68+
),
69+
],
6270
struct: StructPath,
6371
steps: Annotated[int, Option(help="Number of steps in simulation.")] = 0,
6472
timestep: Annotated[float, Option(help="Timestep for integrator, in fs.")] = 1.0,
@@ -380,7 +388,6 @@ def md(
380388
set_read_kwargs_index,
381389
start_summary,
382390
)
383-
from janus_core.helpers.janus_types import Ensembles
384391

385392
# Check options from configuration file are all valid
386393
check_config(ctx)
@@ -404,9 +411,6 @@ def md(
404411
]
405412
)
406413

407-
if ensemble not in get_args(Ensembles):
408-
raise ValueError(f"ensemble must be in {get_args(Ensembles)}")
409-
410414
# Set initial config
411415
all_kwargs = {
412416
"read_kwargs": read_kwargs.copy(),

janus_core/cli/singlepoint.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from __future__ import annotations
44

55
from pathlib import Path
6-
from typing import Annotated
6+
from typing import Annotated, get_args
77

8+
from click import Choice
89
from typer import Context, Option, Typer
910
from typer_config import use_config
1011

@@ -21,6 +22,7 @@
2122
WriteKwargs,
2223
)
2324
from janus_core.cli.utils import yaml_converter_callback
25+
from janus_core.helpers.janus_types import Properties
2426

2527
app = Typer()
2628

@@ -37,6 +39,7 @@ def singlepoint(
3739
properties: Annotated[
3840
list[str] | None,
3941
Option(
42+
click_type=Choice(get_args(Properties)),
4043
help=(
4144
"Properties to calculate. If not specified, 'energy', 'forces' "
4245
"and 'stress' will be returned."

janus_core/cli/types.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
import ast
66
from pathlib import Path
7-
from typing import TYPE_CHECKING, Annotated
7+
from typing import TYPE_CHECKING, Annotated, get_args
88

9+
from click import Choice
910
from typer import Option
1011

12+
from janus_core.helpers.janus_types import Architectures, Devices
13+
1114
if TYPE_CHECKING:
1215
from janus_core.helpers.janus_types import ASEReadArgs
1316

@@ -67,9 +70,19 @@ def __str__(self) -> str:
6770
StructPath = Annotated[Path, Option(help="Path of structure to simulate.")]
6871

6972
Architecture = Annotated[
70-
str | None, Option(help="MLIP architecture to use for calculations.")
73+
str | None,
74+
Option(
75+
click_type=Choice(get_args(Architectures)),
76+
help="MLIP architecture to use for calculations.",
77+
),
78+
]
79+
Device = Annotated[
80+
str | None,
81+
Option(
82+
click_type=Choice(get_args(Devices)),
83+
help="Device to run calculations on.",
84+
),
7185
]
72-
Device = Annotated[str | None, Option(help="Device to run calculations on.")]
7386
ModelPath = Annotated[str | None, Option(help="Path to MLIP model.")]
7487

7588
FilePrefix = Annotated[

0 commit comments

Comments
 (0)