Skip to content

Commit 917cb5c

Browse files
refactor: change parsing of positional args and only take command roots
1 parent 352176e commit 917cb5c

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"program": "${workspaceRoot}/src/azure-cli/azure/cli/__main__.py",
2424
"cwd": "${workspaceRoot}",
2525
"args": [
26-
"VERSION"
26+
"vm",
27+
"show"
2728
],
2829
"console": "externalTerminal"
2930
},

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,12 @@ def execute(self, args):
512512
EVENT_INVOKER_FILTER_RESULT)
513513
from azure.cli.core.commands.events import (
514514
EVENT_INVOKER_PRE_CMD_TBL_TRUNCATE, EVENT_INVOKER_PRE_LOAD_ARGUMENTS, EVENT_INVOKER_POST_LOAD_ARGUMENTS)
515+
from azure.cli.core.util import roughly_parse_command_with_casing
515516

516517
# TODO: Can't simply be invoked as an event because args are transformed
518+
command_preserve_casing = roughly_parse_command_with_casing(args)
517519
args = _pre_command_table_create(self.cli_ctx, args)
518-
raw_args = args.copy()
520+
# The index may be outdated. Make sure the command appears in the loaded command table
519521

520522
self.cli_ctx.raise_event(EVENT_INVOKER_PRE_CMD_TBL_CREATE, args=args)
521523
self.commands_loader.load_command_table(args)
@@ -579,7 +581,7 @@ def execute(self, args):
579581
self.help.show_welcome(subparser)
580582

581583
# TODO: No event in base with which to target
582-
telemetry.set_command_details('az', raw_args=raw_args)
584+
telemetry.set_command_details('az', command_preserve_casing=command_preserve_casing)
583585
telemetry.set_success(summary='welcome')
584586
return CommandResultItem(None, exit_code=0)
585587

@@ -636,7 +638,7 @@ def execute(self, args):
636638
telemetry.set_command_details(self.cli_ctx.data['command'], self.data['output'],
637639
self.cli_ctx.data['safe_params'],
638640
extension_name=extension_name, extension_version=extension_version,
639-
raw_args=raw_args)
641+
command_preserve_casing=command_preserve_casing)
640642
if extension_name:
641643
self.data['command_extension_name'] = extension_name
642644
self.cli_ctx.logging.log_cmd_metadata_extension_info(extension_name, extension_version)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, correlation_id=None, application=None):
5151
self.feedback = None
5252
self.extension_management_detail = None
5353
self.raw_command = None
54-
self.raw_args = None
54+
self.command_preserve_casing = None
5555
self.show_survey_message = False
5656
self.region_input = None
5757
self.region_identified = None
@@ -208,7 +208,7 @@ def _get_azure_cli_properties(self):
208208
set_custom_properties(result, 'InvokeTimeElapsed', str(self.invoke_time_elapsed))
209209
set_custom_properties(result, 'OutputType', self.output_type)
210210
set_custom_properties(result, 'RawCommand', self.raw_command)
211-
set_custom_properties(result, 'RawArgs', ' '.join(self.raw_args or []))
211+
set_custom_properties(result, 'CommandPreserveCasing', self.command_preserve_casing or '')
212212
set_custom_properties(result, 'Params', ','.join(self.parameters or []))
213213
set_custom_properties(result, 'PythonVersion', platform.python_version())
214214
set_custom_properties(result, 'ModuleCorrelation', self.module_correlation)
@@ -439,13 +439,13 @@ def set_extension_management_detail(ext_name, ext_version):
439439

440440

441441
@decorators.suppress_all_exceptions()
442-
def set_command_details(command, output_type=None, parameters=None, extension_name=None, extension_version=None, raw_args=None):
442+
def set_command_details(command, output_type=None, parameters=None, extension_name=None, extension_version=None, command_preserve_casing=None):
443443
_session.command = command
444444
_session.output_type = output_type
445445
_session.parameters = parameters
446446
_session.extension_name = extension_name
447447
_session.extension_version = extension_version
448-
_session.raw_args = raw_args
448+
_session.command_preserve_casing = command_preserve_casing
449449

450450

451451
@decorators.suppress_all_exceptions()

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,17 @@ def roughly_parse_command(args):
12801280
break
12811281
return ' '.join(nouns).lower()
12821282

1283+
def roughly_parse_command_with_casing(args):
1284+
# Roughly parse the command part: <az VM create> --name vm1
1285+
# Similar to knack.invocation.CommandInvoker._rudimentary_get_command, but preserves original casing
1286+
# and we don't need to bother with positional args
1287+
nouns = []
1288+
for arg in args:
1289+
if arg and arg[0] != '-':
1290+
nouns.append(arg)
1291+
else:
1292+
break
1293+
return ' '.join(nouns)
12831294

12841295
def is_guid(guid):
12851296
import uuid

0 commit comments

Comments
 (0)