Skip to content

Commit 6b092ec

Browse files
fix: remove normalize_args method
1 parent ed29aa4 commit 6b092ec

File tree

2 files changed

+12
-117
lines changed

2 files changed

+12
-117
lines changed

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

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def _get_extension_suppressions(mod_loaders):
459459

460460
if use_command_index:
461461
command_index = CommandIndex(self.cli_ctx)
462-
lookup_args = command_index._normalize_args_for_index_lookup(args) # pylint: disable=protected-access
462+
lookup_args = args
463463
index_result = command_index.get(args)
464464
if index_result:
465465
index_modules, index_extensions = index_result
@@ -754,8 +754,6 @@ class CommandIndex:
754754
_HELP_INDEX = 'helpIndex'
755755
_PACKAGED_COMMAND_INDEX_LATEST = 'commandIndex.latest.json'
756756
_PACKAGED_HELP_INDEX_LATEST = 'helpIndex.latest.json'
757-
_LEADING_GLOBAL_OPTS_WITH_VALUE = {'--output', '-o', '--query', '--subscription', '-s', '--tenant', '-t'}
758-
_LEADING_GLOBAL_FLAG_OPTS = {'--debug', '--verbose', '--only-show-errors', '--help', '-h'}
759757

760758
def __init__(self, cli_ctx=None):
761759
"""Class to manage command index.
@@ -998,59 +996,14 @@ def _get_blended_latest_index(self):
998996
logger.debug("Blending packaged core index with local extension index.")
999997
return self._blend_command_indices(core_index, extension_index), extension_index_available, has_non_always_loaded_extensions
1000998

1001-
@classmethod
1002-
def _normalize_args_for_index_lookup(cls, args):
1003-
"""Trim leading global options so index lookup can find the top-level command."""
1004-
if not args:
1005-
return args
1006-
1007-
i = 0
1008-
while i < len(args):
1009-
token = args[i]
1010-
if token == '--':
1011-
return args[i + 1:]
1012-
1013-
if not token.startswith('-'):
1014-
return args[i:]
1015-
1016-
if token.startswith('--'):
1017-
opt_name = token.split('=', 1)[0]
1018-
if '=' in token:
1019-
i += 1
1020-
continue
1021-
if opt_name in cls._LEADING_GLOBAL_OPTS_WITH_VALUE:
1022-
i += 2
1023-
continue
1024-
# Unknown long options are treated as flags here. If invalid, normal parser flow will raise later.
1025-
i += 1
1026-
continue
1027-
1028-
if token in cls._LEADING_GLOBAL_OPTS_WITH_VALUE:
1029-
i += 2
1030-
continue
1031-
1032-
if token in cls._LEADING_GLOBAL_FLAG_OPTS:
1033-
i += 1
1034-
continue
1035-
1036-
# Handle compact short options where value is attached, e.g. -ojson.
1037-
if len(token) > 2 and token[:2] in {'-o', '-s', '-t'}:
1038-
i += 1
1039-
continue
1040-
1041-
# Unknown short options are treated as flags here.
1042-
i += 1
1043-
1044-
return []
1045-
1046999
def get(self, args):
10471000
"""Get the corresponding module and extension list of a command.
10481001
10491002
:param args: command arguments, like ['network', 'vnet', 'create', '-h']
10501003
:return: a tuple containing a list of modules and a list of extensions.
10511004
"""
1052-
normalized_args = self._normalize_args_for_index_lookup(args)
1053-
top_command = normalized_args[0] if normalized_args else None
1005+
1006+
top_command = args[0] if args else None
10541007

10551008
# Resolve effective index.
10561009
# For latest profile, blend packaged core index with local extension index.
@@ -1060,23 +1013,23 @@ def get(self, args):
10601013
if index is not None:
10611014
force_load_all_extensions = has_non_always_loaded_extensions and not extension_index_available and \
10621015
not force_packaged_for_version
1063-
result = self._lookup_command_in_index(index, normalized_args,
1016+
result = self._lookup_command_in_index(index, args,
10641017
force_load_all_extensions=force_load_all_extensions)
10651018
if result:
10661019
return result
10671020

1068-
if normalized_args and not normalized_args[0].startswith('-') and \
1021+
if args and not args[0].startswith('-') and \
10691022
not self.cli_ctx.data['completer_active'] and not force_packaged_for_version and \
10701023
top_command != 'help':
10711024
# Unknown top-level command on latest should prefer extension-only retry and avoid
10721025
# full core module rebuild to preserve packaged-index startup benefit.
10731026
if has_non_always_loaded_extensions:
10741027
logger.debug("No match found in blended latest index for '%s'. Loading all extensions.",
1075-
normalized_args[0])
1028+
args[0])
10761029
return [], None
10771030

10781031
logger.debug("No match found in latest index for '%s' and no dynamic extensions are installed. "
1079-
"Skipping core module rebuild.", normalized_args[0])
1032+
"Skipping core module rebuild.", args[0])
10801033
return [], []
10811034

10821035
logger.debug("No match found in blended latest index. Falling back to local command index.")

src/azure-cli-core/azure/cli/core/tests/test_command_registration.py

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -471,75 +471,21 @@ def test_command_index_uses_packaged_latest_without_seeding(self):
471471
self.assertEqual(list(cmd_tbl), ['hello mod-only', 'hello overridden'])
472472
self.assertEqual(INDEX[CommandIndex._COMMAND_INDEX], {})
473473

474-
@mock.patch('importlib.import_module', _mock_import_lib)
475-
@mock.patch('pkgutil.iter_modules', _mock_iter_modules)
476-
@mock.patch('azure.cli.core.commands._load_command_loader', _mock_load_command_loader)
477-
@mock.patch('azure.cli.core.extension.get_extensions', _mock_no_extensions)
478-
def test_command_index_handles_leading_debug_flag(self):
479-
from azure.cli.core._session import INDEX
480-
from azure.cli.core import CommandIndex, __version__
481-
482-
cli = DummyCli()
483-
loader = cli.commands_loader
484-
485-
INDEX[CommandIndex._COMMAND_INDEX_VERSION] = ""
486-
INDEX[CommandIndex._COMMAND_INDEX_CLOUD_PROFILE] = ""
487-
INDEX[CommandIndex._COMMAND_INDEX] = {}
488-
489-
packaged_index = {
490-
CommandIndex._COMMAND_INDEX_VERSION: __version__,
491-
CommandIndex._COMMAND_INDEX_CLOUD_PROFILE: cli.cloud.profile,
492-
CommandIndex._COMMAND_INDEX: {
493-
'hello': ['azure.cli.command_modules.hello']
494-
}
495-
}
496-
497-
with mock.patch.object(CommandIndex, '_load_packaged_command_index', return_value=packaged_index):
498-
cmd_tbl = loader.load_command_table(["--debug", "hello", "mod-only"])
499-
500-
self.assertEqual(list(cmd_tbl), ['hello mod-only', 'hello overridden'])
501-
self.assertEqual(INDEX[CommandIndex._COMMAND_INDEX], {})
502-
503-
@mock.patch('importlib.import_module', _mock_import_lib)
504-
@mock.patch('pkgutil.iter_modules', _mock_iter_modules)
505-
@mock.patch('azure.cli.core.commands._load_command_loader', _mock_load_command_loader)
506-
@mock.patch('azure.cli.core.extension.get_extensions', _mock_no_extensions)
507-
def test_command_index_handles_leading_output_option(self):
508-
from azure.cli.core._session import INDEX
509-
from azure.cli.core import CommandIndex, __version__
510-
511-
cli = DummyCli()
512-
loader = cli.commands_loader
513-
514-
INDEX[CommandIndex._COMMAND_INDEX_VERSION] = ""
515-
INDEX[CommandIndex._COMMAND_INDEX_CLOUD_PROFILE] = ""
516-
INDEX[CommandIndex._COMMAND_INDEX] = {}
517-
518-
packaged_index = {
519-
CommandIndex._COMMAND_INDEX_VERSION: __version__,
520-
CommandIndex._COMMAND_INDEX_CLOUD_PROFILE: cli.cloud.profile,
521-
CommandIndex._COMMAND_INDEX: {
522-
'hello': ['azure.cli.command_modules.hello']
523-
}
524-
}
525-
526-
with mock.patch.object(CommandIndex, '_load_packaged_command_index', return_value=packaged_index):
527-
cmd_tbl = loader.load_command_table(["-o", "json", "hello", "mod-only"])
528-
529-
self.assertEqual(list(cmd_tbl), ['hello mod-only', 'hello overridden'])
530-
self.assertEqual(INDEX[CommandIndex._COMMAND_INDEX], {})
531-
532474
@mock.patch('importlib.import_module', _mock_import_lib)
533475
@mock.patch('pkgutil.iter_modules', _mock_iter_modules)
534476
@mock.patch('azure.cli.core.commands._load_command_loader', _mock_load_command_loader)
535477
@mock.patch('azure.cli.core.extension.get_extension_modname', _mock_get_extension_modname)
536478
@mock.patch('azure.cli.core.extension.get_extensions', _mock_get_extensions)
537479
def test_command_index_loads_all_extensions_when_overlay_missing(self):
538-
from azure.cli.core._session import INDEX, EXTENSION_INDEX, EXTENSION_HELP_INDEX
480+
from azure.cli.core._session import INDEX, EXTENSION_INDEX
539481
from azure.cli.core import CommandIndex, __version__
540482

541483
cli = DummyCli()
542484
loader = cli.commands_loader
485+
cli.invocation = cli.invocation_cls(cli_ctx=cli,
486+
commands_loader_cls=cli.commands_loader_cls,
487+
parser_cls=cli.parser_cls,
488+
help_cls=cli.help_cls)
543489

544490
INDEX[CommandIndex._COMMAND_INDEX_VERSION] = ""
545491
INDEX[CommandIndex._COMMAND_INDEX_CLOUD_PROFILE] = ""
@@ -565,10 +511,6 @@ def test_command_index_loads_all_extensions_when_overlay_missing(self):
565511
self.assertIn('hello', EXTENSION_INDEX[CommandIndex._COMMAND_INDEX])
566512
self.assertIn('azext_hello1', EXTENSION_INDEX[CommandIndex._COMMAND_INDEX]['hello'])
567513
self.assertIn('azext_hello2', EXTENSION_INDEX[CommandIndex._COMMAND_INDEX]['hello'])
568-
self.assertEqual(EXTENSION_HELP_INDEX[CommandIndex._COMMAND_INDEX_VERSION], __version__)
569-
self.assertEqual(EXTENSION_HELP_INDEX[CommandIndex._COMMAND_INDEX_CLOUD_PROFILE], cli.cloud.profile)
570-
self.assertIn('groups', EXTENSION_HELP_INDEX[CommandIndex._HELP_INDEX])
571-
self.assertIn('commands', EXTENSION_HELP_INDEX[CommandIndex._HELP_INDEX])
572514

573515
@mock.patch('importlib.import_module', _mock_import_lib)
574516
@mock.patch('pkgutil.iter_modules', _mock_iter_modules)

0 commit comments

Comments
 (0)