Skip to content

Commit

Permalink
Fix cmd help extracting and add has_completer in command-change met…
Browse files Browse the repository at this point in the history
…a-export (#461)

* add completer

* set help and example from helpfiles
  • Loading branch information
AllyW authored Aug 21, 2024
1 parent db1212a commit 4f5a0b6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Release History
===============
0.1.73
++++++
* `azdev command-change meta-export`: Add `has_completer` to denote whether completer is configed in arg
* `azdev command-change meta-export`: Extracting arg help and example for loaded HelpFiles

0.1.72
++++++
* Bump `pylint` to 3
Expand Down
2 changes: 1 addition & 1 deletion azdev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# license information.
# -----------------------------------------------------------------------------

__VERSION__ = '0.1.72'
__VERSION__ = '0.1.73'
14 changes: 12 additions & 2 deletions azdev/operations/command_change/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# license information.
# -----------------------------------------------------------------------------

# pylint: disable=no-else-return, too-many-nested-blocks
# pylint: disable=no-else-return, too-many-nested-blocks, too-many-locals, too-many-branches

import time

Expand Down Expand Up @@ -72,6 +72,16 @@ def export_command_meta(modules=None, git_source=None, git_target=None, git_repo
display('Commands loaded in {} sec'.format(stop - start))
command_loader = az_cli.invocation.commands_loader

from azure.cli.core.file_util import get_all_help

help_info = {}
if with_help or with_example:
help_files = get_all_help(az_cli)
for help_item in help_files:
if not help_item.command:
continue
help_info[help_item.command] = help_item

# trim command table to selected_modules
command_loader = filter_modules(command_loader, modules=selected_mod_names)

Expand All @@ -85,7 +95,7 @@ def export_command_meta(modules=None, git_source=None, git_target=None, git_repo
"name": command_name,
"source": _get_command_source(command_name, command),
"is_aaz": False,
"help": command.help,
"help": help_info[command_name] if command_name in help_info else None,
"confirmation": command.confirmation is True,
"arguments": [],
"az_arguments_schema": None,
Expand Down
24 changes: 15 additions & 9 deletions azdev/operations/command_change/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ def normalize_para_type(type_opts, value):
normalize_para_type(type_bool_opts, "bool")


def get_command_examples(command_info, command_meta):
example_items = []
if command_info and command_info.get("help", None) and hasattr(command_info["help"], "examples"):
for example_obj in command_info["help"].examples:
example_items.append({"name": example_obj.name, "text": example_obj.text})
if example_items:
command_meta["examples"] = example_items


def gen_command_meta(command_info, with_help=False, with_example=False):
stored_property_when_exist = ["confirmation", "supports_no_wait", "is_preview", "deprecate_info"]
command_meta = {
Expand All @@ -129,15 +138,10 @@ def gen_command_meta(command_info, with_help=False, with_example=False):
if command_info.get(prop, None):
command_meta[prop] = command_info[prop]
if with_example:
try:
command_meta["examples"] = command_info["help"]["examples"]
except AttributeError:
pass
get_command_examples(command_info, command_meta)
if with_help:
try:
command_meta["desc"] = command_info["help"]["short-summary"]
except AttributeError:
pass
if command_info.get("help", None) and hasattr(command_info["help"], "short_summary"):
command_meta["desc"] = command_info["help"].short_summary
parameters = []
for _, argument in command_info["arguments"].items():
if argument.type is None:
Expand All @@ -163,13 +167,15 @@ def gen_command_meta(command_info, with_help=False, with_example=False):
para["id_part"] = settings["id_part"]
if settings.get("nargs", None):
para["nargs"] = settings["nargs"]
if settings.get("completer", None):
para["has_completer"] = True
if settings.get("default", None):
if not isinstance(settings["default"], (float, int, str, list, bool)):
para["default"] = str(settings["default"])
else:
para["default"] = settings["default"]
if with_help:
para["desc"] = settings["help"]
para["desc"] = settings.get("help", "")
if command_info["is_aaz"] and command_info["az_arguments_schema"]:
process_aaz_argument(command_info["az_arguments_schema"], settings, para)
normalize_para_types(para)
Expand Down

0 comments on commit 4f5a0b6

Please sign in to comment.