Skip to content

Commit ccefff3

Browse files
new: Display example usage in command help pages (#567)
1 parent 3851c3f commit ccefff3

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

linodecli/arg_helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import os
77
import sys
8+
import textwrap
89
from importlib import import_module
910

1011
import requests
@@ -349,13 +350,30 @@ def action_help(cli, command, action):
349350
except ValueError:
350351
return
351352
print(f"linode-cli {command} {action}", end="")
353+
352354
for param in op.params:
353355
pname = param.name.upper()
354356
print(f" [{pname}]", end="")
357+
355358
print()
356359
print(op.summary)
360+
357361
if op.docs_url:
358362
rprint(f"API Documentation: [link={op.docs_url}]{op.docs_url}[/link]")
363+
364+
if len(op.samples) > 0:
365+
print()
366+
print(f"Example Usage{'s' if len(op.samples) > 1 else ''}: ")
367+
368+
rprint(
369+
*[
370+
# Indent all samples for readability; strip and trailing newlines
371+
textwrap.indent(v.get("source").rstrip(), " ")
372+
for v in op.samples
373+
],
374+
sep="\n\n",
375+
)
376+
359377
print()
360378
if op.method == "get" and op.action == "list":
361379
filterable_attrs = [

linodecli/baked/operation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ def __init__(self, command, operation: Operation, method, params):
311311
)
312312
self.docs_url = docs_url
313313

314+
code_samples_ext = operation.extensions.get("code-samples")
315+
self.samples = (
316+
[v for v in code_samples_ext if v.get("lang").lower() == "cli"]
317+
if code_samples_ext is not None
318+
else []
319+
)
320+
314321
@property
315322
def args(self):
316323
"""

requirements-dev.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ requests-mock==1.11.0
88
boto3-stubs[s3]
99
build>=0.10.0
1010
twine>=4.0.2
11-
packaging>=23.2

tests/unit/test_arg_helpers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ def test_action_help_post_method(self, capsys, mocker, mock_cli):
179179
mocked_ops.summary = "test summary"
180180
mocked_ops.docs_url = "https://website.com/endpoint"
181181
mocked_ops.method = "post"
182+
mocked_ops.samples = [
183+
{"lang": "CLI", "source": "linode-cli command action\n --foo=bar"},
184+
{"lang": "CLI", "source": "linode-cli command action\n --bar=foo"},
185+
]
182186

183187
mocked_args = mocker.MagicMock()
184188
mocked_args.read_only = False
@@ -196,6 +200,13 @@ def test_action_help_post_method(self, capsys, mocker, mock_cli):
196200
assert "test summary" in captured.out
197201
assert "API Documentation" in captured.out
198202
assert "https://website.com/endpoint" in captured.out
203+
assert (
204+
"Example Usages: \n"
205+
" linode-cli command action\n"
206+
" --foo=bar\n\n"
207+
" linode-cli command action\n"
208+
" --bar=foo\n\n"
209+
) in captured.out
199210
assert "Arguments" in captured.out
200211
assert "test description" in captured.out
201212
assert "(required)" in captured.out
@@ -208,6 +219,9 @@ def test_action_help_get_method(self, capsys, mocker, mock_cli):
208219
mocked_ops.method = "get"
209220
mocked_ops.action = "list"
210221
mocked_ops.args = None
222+
mocked_ops.samples = [
223+
{"lang": "CLI", "source": "linode-cli command action"}
224+
]
211225

212226
mock_attr = mocker.MagicMock()
213227
mock_attr.filterable = True
@@ -221,6 +235,7 @@ def test_action_help_get_method(self, capsys, mocker, mock_cli):
221235
assert "test summary" in captured.out
222236
assert "API Documentation" in captured.out
223237
assert "https://website.com/endpoint" in captured.out
238+
assert "Example Usage: \n linode-cli command action" in captured.out
224239
assert "Arguments" not in captured.out
225240
assert "filter results" in captured.out
226241
assert "filtername" in captured.out

0 commit comments

Comments
 (0)