diff --git a/linodecli/__init__.py b/linodecli/__init__.py index 2da84752f..00bb6b677 100755 --- a/linodecli/__init__.py +++ b/linodecli/__init__.py @@ -558,6 +558,11 @@ def main(): arg.description, ) ) + if operation.example: + print() + print("Example:") + print(operation.example) + elif operation.method == "get" and parsed.action == "list": filterable_attrs = [ attr for attr in operation.response_model.attrs if attr.filterable diff --git a/linodecli/cli.py b/linodecli/cli.py index eb8909197..7dddffd5d 100644 --- a/linodecli/cli.py +++ b/linodecli/cli.py @@ -209,6 +209,13 @@ def bake(self, spec): continue summary = data[m].get("summary") or "" + example = None + + if "x-code-samples" in data[m]: + for c in data[m]["x-code-samples"]: + if "lang" in c and c["lang"].lower() == "cli" and "source" in c: + example = c["source"] + break use_servers = ( [c["url"] for c in data[m]["servers"]] @@ -356,6 +363,7 @@ def bake(self, spec): use_params, use_servers, allowed_defaults=allowed_defaults, + example=example, ) # remove any empty commands (those that have no actions) diff --git a/linodecli/operation.py b/linodecli/operation.py index ea9c16bba..f958e95c2 100644 --- a/linodecli/operation.py +++ b/linodecli/operation.py @@ -99,7 +99,9 @@ class CLIArg: are defined in a requestBody in the api spec. """ - def __init__(self, name, arg_type, description, path, arg_format, list_item=None): + def __init__( + self, name, arg_type, description, path, arg_format, list_item=None + ): self.name = name self.arg_type = arg_type self.arg_format = arg_format @@ -147,6 +149,7 @@ def __init__( params, servers, allowed_defaults=None, + example=None, ): self.command = command self.action = action @@ -158,6 +161,7 @@ def __init__( self.params = params self.servers = servers self.allowed_defaults = allowed_defaults + self.example = example @property def url(self): @@ -178,6 +182,7 @@ def parse_args(self, args): parser = argparse.ArgumentParser( prog="linode-cli {} {}".format(self.command, self.action), description=self.summary, + example=self.example, ) for param in self.params: parser.add_argument(