Skip to content

Commit 735816f

Browse files
feature: adjustment help formatting
1 parent 348ccf7 commit 735816f

2 files changed

Lines changed: 98 additions & 23 deletions

File tree

src/azure-cli-core/azure/cli/core/__init__.py

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,6 @@ def _get_extension_suppressions(mod_loaders):
440440
# Display help directly from cached data without loading modules
441441
self._display_cached_help(help_index)
442442
# Raise SystemExit to stop execution (similar to how --help normally works)
443-
import sys
444443
sys.exit(0)
445444

446445
if use_command_index:
@@ -530,18 +529,48 @@ def _display_cached_help(self, help_index):
530529
# Show welcome message
531530
print(WELCOME_MESSAGE)
532531

533-
# Display subgroups from cached data
534-
if help_index:
535-
print("Subgroups:")
536-
# Sort and display in the same format as normal help
537-
max_name_len = max(len(name) for name in help_index.keys())
538-
for name in sorted(help_index.keys()):
539-
summary = help_index[name]
540-
padding = ' ' * (max_name_len - len(name))
541-
print(f" {name}{padding} : {summary}")
532+
# Show Group header (to match normal help output)
533+
print("\nGroup")
534+
print(" az")
535+
536+
# Separate groups and commands
537+
groups = help_index.get('groups', {})
538+
commands = help_index.get('commands', {})
539+
540+
# Calculate max name length including tags for proper alignment
541+
def _get_display_len(name, tags):
542+
tag_len = len(tags) + 1 if tags else 0 # +1 for space before tags
543+
return len(name) + tag_len
544+
545+
max_len = 0
546+
if groups:
547+
max_len = max(max_len, max(_get_display_len(name, item.get('tags', '')) for name, item in groups.items()))
548+
if commands:
549+
max_len = max(max_len, max(_get_display_len(name, item.get('tags', '')) for name, item in commands.items()))
550+
551+
# Display subgroups
552+
if groups:
553+
print("\nSubgroups:")
554+
for name in sorted(groups.keys()):
555+
item = groups[name]
556+
tags = item.get('tags', '')
557+
summary = item.get('summary', '')
558+
name_with_tags = f"{name} {tags}" if tags else name
559+
padding = ' ' * (max_len - _get_display_len(name, tags))
560+
print(f" {name_with_tags}{padding} : {summary}")
561+
562+
# Display commands
563+
if commands:
564+
print("\nCommands:")
565+
for name in sorted(commands.keys()):
566+
item = commands[name]
567+
tags = item.get('tags', '')
568+
summary = item.get('summary', '')
569+
name_with_tags = f"{name} {tags}" if tags else name
570+
padding = ' ' * (max_len - _get_display_len(name, tags))
571+
print(f" {name_with_tags}{padding} : {summary}")
542572

543573
print("\nTo search AI knowledge base for examples, use: az find \"az \"")
544-
print("\nFor more specific examples, use: az find \"az <command>\"")
545574

546575
# Show update notification
547576
from azure.cli.core.util import show_updates_available
@@ -563,17 +592,40 @@ def _cache_help_index(self, command_index):
563592
help_file = CliGroupHelpFile(self.cli_ctx.invocation.help, '', subparser)
564593
help_file.load(subparser)
565594

566-
# Extract summaries from help file's children
567-
help_index_data = {}
595+
# Helper to build tag string for an item
596+
def _get_tags(item):
597+
tags = []
598+
if hasattr(item, 'deprecate_info') and item.deprecate_info:
599+
tags.append(str(item.deprecate_info.tag))
600+
if hasattr(item, 'preview_info') and item.preview_info:
601+
tags.append(str(item.preview_info.tag))
602+
if hasattr(item, 'experimental_info') and item.experimental_info:
603+
tags.append(str(item.experimental_info.tag))
604+
return ' '.join(tags)
605+
606+
# Separate groups and commands
607+
groups = {}
608+
commands = {}
609+
568610
for child in help_file.children:
569611
if hasattr(child, 'name') and hasattr(child, 'short_summary'):
570-
if ' ' not in child.name: # Only top-level commands
571-
help_index_data[child.name] = child.short_summary
612+
if ' ' not in child.name: # Only top-level items
613+
tags = _get_tags(child)
614+
item_data = {
615+
'summary': child.short_summary,
616+
'tags': tags
617+
}
618+
# Check if it's a group or command
619+
if child.type == 'group':
620+
groups[child.name] = item_data
621+
else:
622+
commands[child.name] = item_data
572623

573624
# Store in the command index
574-
if help_index_data:
625+
help_index_data = {'groups': groups, 'commands': commands}
626+
if groups or commands:
575627
command_index.INDEX[command_index._HELP_INDEX] = help_index_data
576-
logger.debug("Cached %d help entries", len(help_index_data))
628+
logger.debug("Cached %d groups and %d commands", len(groups), len(commands))
577629
except Exception as ex: # pylint: disable=broad-except
578630
logger.debug("Failed to cache help data: %s", ex)
579631

src/azure-cli-core/azure/cli/core/commands/__init__.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -592,19 +592,42 @@ def execute(self, args):
592592
help_file = CliGroupHelpFile(self.cli_ctx.help, '', subparser)
593593
help_file.load(subparser)
594594

595-
# Build help index from the help file's children
596-
help_index_data = {}
595+
# Helper to build tag string for an item
596+
def _get_tags(item):
597+
tags = []
598+
if hasattr(item, 'deprecate_info') and item.deprecate_info:
599+
tags.append(str(item.deprecate_info.tag))
600+
if hasattr(item, 'preview_info') and item.preview_info:
601+
tags.append(str(item.preview_info.tag))
602+
if hasattr(item, 'experimental_info') and item.experimental_info:
603+
tags.append(str(item.experimental_info.tag))
604+
return ' '.join(tags)
605+
606+
# Separate groups and commands
607+
groups = {}
608+
commands = {}
609+
597610
for child in help_file.children:
598611
if hasattr(child, 'name') and hasattr(child, 'short_summary'):
599-
if ' ' not in child.name: # Only top-level commands
600-
help_index_data[child.name] = child.short_summary
612+
if ' ' not in child.name: # Only top-level items
613+
tags = _get_tags(child)
614+
item_data = {
615+
'summary': child.short_summary,
616+
'tags': tags
617+
}
618+
# Check if it's a group or command
619+
if child.type == 'group':
620+
groups[child.name] = item_data
621+
else:
622+
commands[child.name] = item_data
601623

602624
# Store in the command index
603-
if help_index_data:
625+
help_index_data = {'groups': groups, 'commands': commands}
626+
if groups or commands:
604627
from azure.cli.core._session import INDEX
605628
from azure.cli.core import __version__
606629
INDEX['helpIndex'] = help_index_data
607-
logger.debug("Cached %d help entries for fast access", len(help_index_data))
630+
logger.debug("Cached %d groups and %d commands for fast access", len(groups), len(commands))
608631
except Exception as ex: # pylint: disable=broad-except
609632
logger.debug("Failed to cache help data: %s", ex)
610633

0 commit comments

Comments
 (0)