Skip to content

API Reference

pypfmt

A Python package to sort and format pyproject.toml (pypfmt).

CLI Module

pypfmt.cli

Command-line interface for pypfmt.

main(files=None, check=False, diff=False, version=None)

Sort and format pyproject.toml files.

Source code in src/pypfmt/cli.py
@app.command()
def main(
    files: Annotated[
        list[str] | None,
        typer.Argument(help="pyproject.toml files to format"),
    ] = None,
    check: Annotated[
        bool,
        typer.Option(
            "--check", help="Check if files are formatted, exit non-zero if not"
        ),
    ] = False,
    diff: Annotated[
        bool,
        typer.Option("--diff", help="Show unified diff of changes"),
    ] = False,
    version: Annotated[  # noqa: ARG001
        bool | None,
        typer.Option(
            "--version",
            "-v",
            callback=_version_callback,
            is_eager=True,
        ),
    ] = None,
) -> None:
    """Sort and format pyproject.toml files."""
    if not files:
        if sys.stdin.isatty():
            # Interactive terminal with no files -- show usage
            typer.echo("error: no input files provided", err=True)
            typer.echo("Usage: pypfmt [OPTIONS] [FILES]...", err=True)
            typer.echo("  or pipe input: cat pyproject.toml | pypfmt", err=True)
            raise typer.Exit(code=2)
        # Non-TTY: check if stdin actually has data available
        if not _stdin_has_data():
            typer.echo("error: no input files provided", err=True)
            raise typer.Exit(code=2)
        # Stdin mode (piped input available)
        raise typer.Exit(code=_process_stdin(check=check, diff=diff))

    # File mode
    exit_code = 0
    for filepath in files:
        code = _process_file(filepath, check=check, diff=diff)
        exit_code = max(exit_code, code)
    raise typer.Exit(code=exit_code)