CLI surface¶
The CLI is the un-comtrade console script — installed alongside
the Python package. Five outer commands, 22 sub-subcommands, and
five output formats (json, table, csv, markdown, text).
API reference¶
The full CLI surface is generated from the SDK's docstrings via mkdocstrings. The CLI is a thin wrapper around the Python facade; there is no separate API beyond the Python facade.
cli
¶
Top-level package for the un-comtrade CLI.
The CLI is the first consumer of the public SDK
API surface (per 031_PRODUCTION_READINESS.md §9
and docs/IMPLEMENTATION_BASELINE_v1.md §7
"Application").
The foundation phase (C-001) ships:
- A argparse-based root parser
(:func:
un_comtrade.cli.main.build_parser). - A
main(argv)entry point (:func:un_comtrade.cli.main.main). - Exit codes (:data:
EXIT_OK, ...). - Configuration loader
(:func:
un_comtrade.cli.utils.load_cli_configuration). - Output formatters: JSON (functional), TABLE and CSV (placeholders for P7-002+).
- Zero business commands. The CLI is ready for
P7-002 to register
metadata,trade,storage,etl, andanalyticscommands.
Public surface (re-exported for convenience):
- :func:
build_parser - :func:
main - :data:
EXIT_OK/ :data:EXIT_GENERIC_ERROR/ :data:EXIT_USER_ERROR/ :data:EXIT_CONFIG_ERROR/ :data:EXIT_NETWORK_ERROR/ :data:EXIT_AUTH_ERROR - :class:
CLIError/ :class:CLIConfigurationError - :func:
load_cli_configuration - :data:
OUTPUT_FORMATS
CLIConfigurationError
¶
Bases: CLIError
Raised when the CLI cannot load or apply
a configuration (missing API key, malformed
~/.un_comtrade config, etc.).
CLIError
¶
Bases: ComtradeError
Base class for CLI-raised errors.
Inherits from :class:ComtradeError so that
callers may catch SDK and CLI errors with the
same except clause.
ProgressReporter
¶
Minimal progress reporter.
Output shape::
[trade/exports] page 1: 250 records (total 250)
The reporter writes to stderr (not stdout) so
that the data stream on stdout remains
machine-readable when --output-format
json is used.
Source code in un_comtrade/cli/utils/progress.py
update
¶
Update the running total and (optionally) the page number. Writes a single line to the configured stream.
Source code in un_comtrade/cli/utils/progress.py
finish
¶
build_parser
¶
build_parser(
*,
prog: str = "un-comtrade",
description: str = "Command-line interface for the un-comtrade-sdk. The CLI is a thin consumer of the SDK's public API surface; the SDK itself is fully usable from Python.",
) -> ArgumentParser
Construct the root argparse parser.
Returns a parser with global options and
subparsers for every registered command. Tests
use this to construct the parser without
running :func:main.
Source code in un_comtrade/cli/main.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
load_cli_configuration
¶
load_cli_configuration(
*,
api_key: str | None = None,
log_level: str | None = None,
base_url: str | None = None,
timeout: float | None = None,
) -> Configuration
Load the SDK :class:Configuration from
the environment, applying CLI-side overrides
on top.
Parameters¶
api_key
Override for the subscription key. When
supplied, this takes precedence over the
UN_COMTRADE_KEY env var.
log_level
Override for the log level. When supplied,
this takes precedence over the
UN_COMTRADE_LOG_LEVEL env var.
base_url
Override for the upstream base URL.
timeout
Override for the per-request timeout
(seconds).
Returns¶
Configuration
A frozen :class:Configuration with all
overrides applied.
Raises¶
CLIConfigurationError When an override value is invalid (unknown log level, malformed URL, etc.).
Source code in un_comtrade/cli/utils/config_loader.py
load_dataset
¶
Load a :class:CanonicalDataset from a
previously-stored file via the public Storage
API.
Parameters¶
path Path to a stored dataset. The extension determines the backend.
Returns¶
CanonicalDataset The deserialised dataset.
Raises¶
CLIConfigurationError When the path does not exist, the extension is unsupported, or the backend cannot read the file.
Source code in un_comtrade/cli/utils/dataset_loader.py
make_progress_reporter
¶
make_progress_reporter(
*,
label: str,
enabled: bool,
force: bool = False,
stream=None,
) -> ProgressReporter
Factory that returns either a real
:class:ProgressReporter or a no-op stub.
Always returns a reporter object so callers
can invoke .update(...) unconditionally.
Source code in un_comtrade/cli/utils/progress.py
render_to_destination
¶
Write text to output (a file path)
or to stdout when output is None.
Raises¶
CLIConfigurationError
When output cannot be opened for
writing (permission, missing parent dir,
etc.).
Source code in un_comtrade/cli/utils/output.py
Examples¶
Related Recipes¶
- RECIPE-091 — Drive metadata commands from the CLI.
- RECIPE-095 — Drive trade commands from the CLI.
- RECIPE-099 — Drive analytics commands from the CLI.
Related Guides¶
- CLI → Index — full CLI command reference.