Skip to content

Commit d4809c2

Browse files
feature: adding top-level command lookup from commandIndex.json
1 parent b3f450e commit d4809c2

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,25 @@ def _get_extension_suppressions(mod_loaders):
438438
print(f"[PERF] index_result: {index_result}", file=sys.stderr, flush=True)
439439
if index_result:
440440
index_modules, index_extensions = index_result
441+
442+
# Special case for top-level completion - create minimal command groups
443+
if index_modules == '__top_level_completion__':
444+
import time
445+
from azure.cli.core.commands import AzCliCommand
446+
start_time = time.time()
447+
print(f"[PERF] Top-level completion mode: creating {len(index_extensions)} command stubs", file=sys.stderr, flush=True)
448+
# index_extensions contains the command names, not extensions
449+
for cmd_name in index_extensions:
450+
# Create a minimal command entry for tab completion
451+
# This allows argparse to see the command without loading the module
452+
if cmd_name not in self.command_table:
453+
self.command_table[cmd_name] = AzCliCommand(
454+
self, cmd_name, lambda: None
455+
)
456+
elapsed = time.time() - start_time
457+
print(f"[PERF] Created command stubs in {elapsed:.3f} seconds", file=sys.stderr, flush=True)
458+
return self.command_table
459+
441460
# Always load modules and extensions, because some of them (like those in
442461
# ALWAYS_LOADED_EXTENSIONS) don't expose a command, but hooks into handlers in CLI core
443462
import time
@@ -619,8 +638,14 @@ def get(self, args):
619638
return None
620639

621640
# Make sure the top-level command is provided, like `az version`.
622-
# Skip command index for `az` or `az --help`.
641+
# For top-level completion (az [tab]), use a special marker to skip module loading
623642
if not args or args[0].startswith('-'):
643+
if not args and self.cli_ctx.data.get('completer_active'):
644+
# Return a special marker so we know to skip module loading for top-level completion
645+
index = self.INDEX[self._COMMAND_INDEX]
646+
all_commands = list(index.keys())
647+
logger.debug("Top-level completion: %d commands available", len(all_commands))
648+
return '__top_level_completion__', all_commands # special marker, command list
624649
return None
625650

626651
# Get the top-level command, like `network` in `network vnet create -h`

0 commit comments

Comments
 (0)