Modern, extensible Python build backend implementing PEP 517/660 standards with plugin architecture.
—
Command-line interface providing build, metadata inspection, version management, and dependency analysis functionality. The CLI offers direct access to hatchling's capabilities for development and debugging.
def hatchling() -> int:
"""
Main CLI entry point.
Returns:
int: Exit code (0 for success)
"""Build distributions (wheels and source distributions) from the command line.
hatchling build [OPTIONS]
Options:
-t, --target TEXT Build target (wheel, sdist, binary, or custom)
-c, --clean Clean build directory before building
--ext Build extensions
--no-sources Skip source distribution building
--no-wheel Skip wheel building
-h, --help Show help messageInspect and display project metadata information.
hatchling metadata [OPTIONS]
Options:
--field TEXT Show specific metadata field
--core Show core metadata in email format
--json Output metadata as JSON
-h, --help Show help messageManage project version information.
hatchling version [OPTIONS] [VERSION]
Arguments:
VERSION New version to set (optional)
Options:
--dry-run Show what would be changed without making changes
-h, --help Show help messageAnalyze and display project dependencies.
hatchling dep [OPTIONS] COMMAND [ARGS]...
Commands:
show Show dependency information
hash Show dependency hashes
freeze Show installed dependencies in freeze format
Options:
-h, --help Show help messageBuild all distribution types:
hatchling buildBuild only wheels:
hatchling build -t wheelBuild only source distributions:
hatchling build -t sdistBuild with clean directory:
hatchling build --cleanBuild custom targets:
hatchling build -t my-custom-builderShow all metadata:
hatchling metadataShow specific field:
hatchling metadata --field name
hatchling metadata --field version
hatchling metadata --field dependenciesShow core metadata:
hatchling metadata --coreOutput as JSON:
hatchling metadata --jsonShow current version:
hatchling versionSet new version:
hatchling version 1.2.0Preview version change:
hatchling version 1.2.0 --dry-runShow project dependencies:
hatchling dep showShow dependency hashes:
hatchling dep hashShow freeze format:
hatchling dep freezedef build_command(subparsers, defaults):
"""Register build command with argument parser."""
def metadata_command(subparsers, defaults):
"""Register metadata command with argument parser."""
def version_command(subparsers, defaults):
"""Register version command with argument parser."""
def dep_command(subparsers, defaults):
"""Register dependency command with argument parser."""The CLI uses Python's argparse module with the following structure:
parser = argparse.ArgumentParser(prog='hatchling', allow_abbrev=False)
subparsers = parser.add_subparsers()
# Default settings for all subcommands
defaults = {'metavar': ''}# Check project metadata
hatchling metadata --field name
hatchling metadata --field version
# Build development distributions
hatchling build --clean
# Update version
hatchling version 1.1.0
# Build release distributions
hatchling build -t wheel -t sdist# Validate metadata
hatchling metadata --core > /dev/null
# Build distributions for upload
hatchling build --clean
# Check dependency information
hatchling dep show# Show all metadata in JSON format
hatchling metadata --json
# Show specific metadata fields
hatchling metadata --field dynamic
hatchling metadata --field build-system
# Preview version changes
hatchling version 2.0.0 --dry-run# Build with custom builder plugin
hatchling build -t my-custom-builder
# Show available builders (through metadata)
hatchling metadata --field tool.hatch.buildThe CLI returns standard exit codes:
0: Success1: General error (invalid arguments, build failures, etc.)2: File not found or permission errorCLI behavior can be configured through:
Common environment variables that affect CLI behavior:
HATCH_BUILD_CLEAN: Clean build directory (equivalent to --clean)HATCH_BUILD_NO_SOURCES: Skip source distribution buildingHATCH_BUILD_NO_WHEEL: Skip wheel buildingHATCH_VERBOSE: Enable verbose outputThe CLI reads configuration from:
[tool.hatch.build]
targets = ["wheel", "sdist"]
clean = true
[tool.hatch.version]
source = "code"
path = "src/package/__about__.py"
[tool.hatch.metadata]
allow-direct-references = trueThe CLI functions can also be called programmatically:
import sys
from hatchling.cli import hatchling
# Call CLI programmatically
sys.argv = ['hatchling', 'build', '--clean']
exit_code = hatchling()However, for programmatic usage, it's recommended to use the builder classes directly rather than the CLI interface.
The CLI provides user-friendly error messages for common issues:
Errors are displayed to stderr with helpful suggestions when possible.
The CLI integrates with various development tools:
Example Makefile integration:
build:
hatchling build --clean
metadata:
hatchling metadata --json > metadata.json
version:
hatchling version $(VERSION)The CLI interface provides a convenient way to access hatchling's functionality during development, CI/CD, and release workflows while maintaining consistency with the programmatic API.
Install with Tessl CLI
npx tessl i tessl/pypi-hatchling