1
1
import asyncio
2
- import json
3
2
from pathlib import Path
4
- from typing import Any , get_args
3
+ from typing import get_args
5
4
6
5
import click
7
6
from pydantic import ValidationError
12
11
from guidellm .benchmark .scenario import GenerativeTextScenario
13
12
from guidellm .config import print_config
14
13
from guidellm .scheduler import StrategyType
14
+ from guidellm .utils import cli as cli_tools
15
15
16
16
STRATEGY_PROFILE_CHOICES = set (
17
17
list (get_args (ProfileType )) + list (get_args (StrategyType ))
18
18
)
19
19
20
20
21
- def parse_json (ctx , param , value ): # noqa: ARG001
22
- if value is None :
23
- return None
24
- try :
25
- return json .loads (value )
26
- except json .JSONDecodeError as err :
27
- raise click .BadParameter (f"{ param .name } must be a valid JSON string." ) from err
28
-
29
-
30
- def set_if_not_default (ctx : click .Context , ** kwargs ) -> dict [str , Any ]:
31
- """
32
- Set the value of a click option if it is not the default value.
33
- This is useful for setting options that are not None by default.
34
- """
35
- values = {}
36
- for k , v in kwargs .items ():
37
- if ctx .get_parameter_source (k ) != click .core .ParameterSource .DEFAULT :
38
- values [k ] = v
39
-
40
- return values
41
-
42
-
43
21
@click .group ()
44
22
def cli ():
45
23
pass
@@ -50,7 +28,10 @@ def cli():
50
28
)
51
29
@click .option (
52
30
"--scenario" ,
53
- type = str ,
31
+ type = cli_tools .Union (
32
+ click .Path (exists = True , readable = True , file_okay = True , dir_okay = False ),
33
+ click .STRING
34
+ ),
54
35
default = None ,
55
36
help = ("TODO: A scenario or path to config" ),
56
37
)
@@ -70,7 +51,7 @@ def cli():
70
51
)
71
52
@click .option (
72
53
"--backend-args" ,
73
- callback = parse_json ,
54
+ callback = cli_tools . parse_json ,
74
55
default = GenerativeTextScenario .get_default ("backend_args" ),
75
56
help = (
76
57
"A JSON string containing any arguments to pass to the backend as a "
@@ -99,7 +80,7 @@ def cli():
99
80
@click .option (
100
81
"--processor-args" ,
101
82
default = GenerativeTextScenario .get_default ("processor_args" ),
102
- callback = parse_json ,
83
+ callback = cli_tools . parse_json ,
103
84
help = (
104
85
"A JSON string containing any arguments to pass to the processor constructor "
105
86
"as a dict with **kwargs."
@@ -117,7 +98,7 @@ def cli():
117
98
@click .option (
118
99
"--data-args" ,
119
100
default = GenerativeTextScenario .get_default ("data_args" ),
120
- callback = parse_json ,
101
+ callback = cli_tools . parse_json ,
121
102
help = (
122
103
"A JSON string containing any arguments to pass to the dataset creation "
123
104
"as a dict with **kwargs."
@@ -218,7 +199,7 @@ def cli():
218
199
)
219
200
@click .option (
220
201
"--output-extras" ,
221
- callback = parse_json ,
202
+ callback = cli_tools . parse_json ,
222
203
help = "A JSON string of extra data to save with the output benchmarks" ,
223
204
)
224
205
@click .option (
@@ -263,7 +244,7 @@ def benchmark(
263
244
):
264
245
click_ctx = click .get_current_context ()
265
246
266
- overrides = set_if_not_default (
247
+ overrides = cli_tools . set_if_not_default (
267
248
click_ctx ,
268
249
target = target ,
269
250
backend_type = backend_type ,
0 commit comments