Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Display example usage in command help pages #567

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions linodecli/arg_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import os
import sys
import textwrap
from importlib import import_module

import requests
Expand Down Expand Up @@ -349,13 +350,30 @@ def action_help(cli, command, action):
except ValueError:
return
print(f"linode-cli {command} {action}", end="")

for param in op.params:
pname = param.name.upper()
print(f" [{pname}]", end="")

print()
print(op.summary)

if op.docs_url:
rprint(f"API Documentation: [link={op.docs_url}]{op.docs_url}[/link]")

if len(op.samples) > 0:
print()
print(f"Example Usage{'s' if len(op.samples) > 1 else ''}: ")

rprint(
*[
# Indent all samples for readability; strip and trailing newlines
textwrap.indent(v.get("source").rstrip(), " ")
for v in op.samples
],
sep="\n\n",
)

print()
if op.method == "get" and op.action == "list":
filterable_attrs = [
Expand Down
7 changes: 7 additions & 0 deletions linodecli/baked/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ def __init__(self, command, operation: Operation, method, params):
)
self.docs_url = docs_url

code_samples_ext = operation.extensions.get("code-samples")
self.samples = (
[v for v in code_samples_ext if v.get("lang").lower() == "cli"]
if code_samples_ext is not None
else []
)

@property
def args(self):
"""
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ requests-mock==1.11.0
boto3-stubs[s3]
build>=0.10.0
twine>=4.0.2
packaging>=23.2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already defined in requirements.txt 👍

15 changes: 15 additions & 0 deletions tests/unit/test_arg_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ def test_action_help_post_method(self, capsys, mocker, mock_cli):
mocked_ops.summary = "test summary"
mocked_ops.docs_url = "https://website.com/endpoint"
mocked_ops.method = "post"
mocked_ops.samples = [
{"lang": "CLI", "source": "linode-cli command action\n --foo=bar"},
{"lang": "CLI", "source": "linode-cli command action\n --bar=foo"},
]

mocked_args = mocker.MagicMock()
mocked_args.read_only = False
Expand All @@ -196,6 +200,13 @@ def test_action_help_post_method(self, capsys, mocker, mock_cli):
assert "test summary" in captured.out
assert "API Documentation" in captured.out
assert "https://website.com/endpoint" in captured.out
assert (
"Example Usages: \n"
" linode-cli command action\n"
" --foo=bar\n\n"
" linode-cli command action\n"
" --bar=foo\n\n"
) in captured.out
assert "Arguments" in captured.out
assert "test description" in captured.out
assert "(required)" in captured.out
Expand All @@ -208,6 +219,9 @@ def test_action_help_get_method(self, capsys, mocker, mock_cli):
mocked_ops.method = "get"
mocked_ops.action = "list"
mocked_ops.args = None
mocked_ops.samples = [
{"lang": "CLI", "source": "linode-cli command action"}
]

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