Skip to content

Commit e8b627b

Browse files
Add help page
1 parent 92f5cc8 commit e8b627b

File tree

2 files changed

+66
-21
lines changed

2 files changed

+66
-21
lines changed

linodecli/baked/request.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _parse_request_model(
132132
prefix: Optional[str] = None,
133133
parent: Optional[str] = None,
134134
depth: int = 0,
135-
option_path: Optional[List[str]] = None,
135+
option_path: List[str] = None,
136136
) -> List[OpenAPIRequestArg]:
137137
"""
138138
Parses an OpenAPI schema into a list of OpenAPIRequest objects
@@ -148,10 +148,31 @@ def _parse_request_model(
148148
:returns: The flattened request model, as a list
149149
:rtype: list[OpenAPIRequestArg]
150150
"""
151+
152+
if option_path is None:
153+
option_path = []
154+
151155
args = []
152156

157+
if isinstance(schema, dict):
158+
schema = Schema(schema["path"], schema, schema._root)
159+
160+
for i, entry in enumerate(schema.oneOf or []):
161+
if isinstance(entry, dict):
162+
entry = Schema(schema.path + ["oneOf", i + 1], entry, schema._root)
163+
164+
args += _parse_request_model(
165+
entry,
166+
prefix=prefix,
167+
parent=parent,
168+
depth=depth,
169+
option_path=option_path + [entry.title],
170+
)
171+
172+
if schema.oneOf is not None:
173+
return args
174+
153175
properties, required = _aggregate_schema_properties(schema)
154-
print(properties, required)
155176

156177
if properties is None:
157178
return args
@@ -173,6 +194,7 @@ def _parse_request_model(
173194
# NOTE: We do not increment the depth because dicts do not have
174195
# parent arguments.
175196
depth=depth,
197+
option_path=option_path,
176198
)
177199

178200
# Handle arrays of objects that not marked as JSON
@@ -205,6 +227,7 @@ def _parse_request_model(
205227
prefix=pref,
206228
parent=pref,
207229
depth=depth + 1,
230+
option_path=option_path,
208231
)
209232
else:
210233
args.append(

linodecli/help_pages.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
import textwrap
88
from collections import defaultdict
9-
from typing import Dict, List, Optional
9+
from typing import Dict, List, Optional, Tuple
1010

1111
from rich import box
1212
from rich import print as rprint
@@ -264,32 +264,42 @@ def _help_action_print_body_args(
264264
console.print(f"[bold]Arguments{f' ({title})' if title else ''}:[/]")
265265

266266
for group in _help_group_arguments(args):
267-
for arg in group:
268-
metadata = []
267+
for option in _help_group_options(group):
268+
if option[0] is not None:
269+
# If this is an option with an explicit title,
270+
# display it here
271+
console.print(
272+
Padding.indent(f"[bold underline]{option[0]}[/]:", 4),
273+
)
269274

270-
if op.method in {"post", "put"} and arg.required:
271-
metadata.append("required")
275+
for arg in option[1]:
276+
metadata = []
272277

273-
if arg.format == "json":
274-
metadata.append("JSON")
278+
if op.method in {"post", "put"} and arg.required:
279+
metadata.append("required")
275280

276-
if arg.nullable:
277-
metadata.append("nullable")
281+
if arg.format == "json":
282+
metadata.append("JSON")
278283

279-
if arg.is_parent:
280-
metadata.append("conflicts with children")
284+
if arg.nullable:
285+
metadata.append("nullable")
281286

282-
prefix = f" ({', '.join(metadata)})" if len(metadata) > 0 else ""
287+
if arg.is_parent:
288+
metadata.append("conflicts with children")
283289

284-
arg_text = Text.from_markup(
285-
f"[bold green]--{arg.path}[/][bold]{prefix}:[/] {arg.description_rich}"
286-
)
290+
prefix = (
291+
f" ({', '.join(metadata)})" if len(metadata) > 0 else ""
292+
)
287293

288-
console.print(
289-
Padding.indent(arg_text, (arg.depth * 2) + 2),
290-
)
294+
arg_text = Text.from_markup(
295+
f"[bold green]--{arg.path}[/][bold]{prefix}:[/] {arg.description_rich}"
296+
)
291297

292-
console.print()
298+
console.print(
299+
Padding.indent(arg_text, (arg.depth * 2) + 2),
300+
)
301+
302+
console.print()
293303

294304

295305
def _help_group_arguments(
@@ -346,3 +356,15 @@ def _help_group_arguments(
346356
result += groups
347357

348358
return result
359+
360+
361+
def _help_group_options(
362+
args: List[OpenAPIRequestArg],
363+
) -> List[Tuple[Optional[str], List[OpenAPIRequestArg]]]:
364+
groups = defaultdict(list)
365+
366+
for arg in args:
367+
group_key = " - ".join(arg.option_path) if arg.option_path else None
368+
groups[group_key].append(arg)
369+
370+
return sorted(groups.items(), key=lambda v: v[0] or "")

0 commit comments

Comments
 (0)