Introducing a new way to review your PRs.

Read the announcement

azure / azure-cli

Azure Command-Line Interface

View on GitHub

13 documents · Updated Nov 16, 2026

docs / cli-application-entrypoints

CLI application entrypoints

Overview

The packaged Azure CLI starts through src/…/__main__.py, which is the Python module entrypoint for the az process. Import time records start_time, creates the default CLI object with azure.cli.core.get_default_cli(), registers the EVENT_CLI_PRE_EXECUTE hook from Knack to run azure.cli.core._output.apply_output_newline, and initializes telemetry with telemetry.set_application(az_cli, ARGCOMPLETE_ENV_NAME). Command execution is intentionally thin: cli_main(cli, args) delegates to cli.invoke(args), and the module passes sys.argv[1:] into that function before exiting with the returned status code.

src/…/__main__.py also owns the process-level cleanup around invocation. It starts telemetry before calling cli.invoke, marks success only for exit code 0, converts KeyboardInterrupt into exit code 1, lets SystemExit propagate while recording its code, and always logs total/init/invoke elapsed time. The finally block performs two user-facing post-run behaviors: it may run az upgrade automatically when the command succeeded and the auto-upgrade config enables it, and it calls prompt_survey_message(az_cli) before recording timing fields and concluding telemetry. Both post-run paths catch broad failures so they do not replace the command's original result.

The top-level package initializer in src/…/__init__.py only extends the package path with pkgutil.extend_path. That makes azure.cli a namespace-style package so independently installed Azure CLI pieces can contribute modules under the same package name while the built-in CLI package still provides azure.cli.__main__.

Built-in command-module discovery is exposed by the presence of src/…/__init__.py. The file is empty, but it marks azure.cli.command_modules as an importable package namespace for the command modules bundled with the Azure CLI distribution. Command loading itself is handled downstream by CLI core code; this doc's coverage stops at the package namespace bootstrap.

Packaging support for the process entrypoint is declared adjacent to the package in src/azure-cli/setup.cfg, whose [build_py] section lists azure.cli,__main__,azure/cli/__main__.py as an extra build source file. The assignment's historical cover path src/…/setup.py is not present at HEAD, so the active package build hint for this scope is setup.cfg rather than a setup script. src/azure-cli/README.rst documents the user-visible entry shape as az [ group ] [ subgroup ] [ command ] {parameters} and points users to az configure, help flags, tab completion, output querying, feedback, and release notes; it does not define runtime behavior.

Routing/coverage note: route changes here when they alter how az enters azure.cli.__main__, how the azure.cli or azure.cli.command_modules package namespaces are exposed, or how the package metadata describes the CLI entrypoint. Command implementation, command discovery internals, telemetry implementation, config storage, and extension loading belong to the imported core packages or command-module docs, not this entrypoint doc.

Coupling

  • If src/…/__main__.py changes the call to get_default_cli() or the cli.invoke(sys.argv[1:]) routing, verify the package/build entrypoint still starts the same module; otherwise the installed az command can initialize telemetry and hooks but invoke a different argument path than documented by src/azure-cli/README.rst.
  • If the package initializer src/…/__init__.py stops extending __path__, verify independently installed packages that contribute under azure.cli still import correctly. Removing the namespace extension can make contributed CLI subpackages disappear even though the top-level az process still starts.
  • If src/…/__init__.py is removed or gains side effects, verify the command-module loader in CLI core still treats azure.cli.command_modules as the built-in command-module namespace. This file currently provides only importability; adding runtime work here would make every built-in command-module discovery pay that cost.
  • If the post-run auto-upgrade conditions in src/…/__main__.py change, verify commands with too few sys.argv elements, az upgrade, and az extension update remain excluded or safely handled. The current code relies on IndexError handling around direct sys.argv[1] and sys.argv[2] access.

Risks

  • src/…/__main__.py indexes sys.argv[1] and sys.argv[2] inside the auto-upgrade guard after command execution. The surrounding except IndexError: pass prevents crashes for short invocations, but changing that guard without preserving the protection can make successful bare or malformed az invocations fail during cleanup rather than returning their original result.
  • src/…/__main__.py runs subprocess.call(['az', 'upgrade', ...]) from inside the entrypoint when auto-upgrade is enabled. If the exclusion for upgrade is removed or the invoked command name is changed incorrectly, a successful command can recursively spawn another az process or run the wrong executable during cleanup.