Command Line Interface
AnyDI provides a CLI tool to validate and visualize your dependency graph.
Basic Usage
anydi myapp.container:container
This command loads the container from myapp/container.py and prints the dependency tree.
You can also use a factory function that returns a container:
anydi myapp.container:create_container
Output Formats
Tree (Default)
Shows dependencies as a tree:
anydi myapp:container
Output:
UserService (singleton)
├── repo: UserRepository (singleton)
│ └── db: Database (singleton)
└── cache: Cache (singleton)
Mermaid
Generate Mermaid diagram:
anydi myapp:container -o mermaid
Output:
graph TD
UserService["UserService (singleton)"] -->|repo| UserRepository["UserRepository (singleton)"]
UserRepository["UserRepository (singleton)"] -->|db| Database["Database (singleton)"]
UserService["UserService (singleton)"] -->|cache| Cache["Cache (singleton)"]
Render with mermaid-cli
Install mermaid-cli:
npm install -g @mermaid-js/mermaid-cli
Generate PNG image:
anydi myapp:container -o mermaid | mmdc -i - -o graph.png
Generate SVG:
anydi myapp:container -o mermaid | mmdc -i - -o graph.svg
DOT (Graphviz)
Generate Graphviz DOT format:
anydi myapp:container -o dot
Output:
digraph G {
node [shape=box];
"UserService (singleton)" -> "UserRepository (singleton)" [label="repo"];
"UserRepository (singleton)" -> "Database (singleton)" [label="db"];
"UserService (singleton)" -> "Cache (singleton)" [label="cache"];
}
Render with Graphviz
Install Graphviz:
brew install graphviz
apt install graphviz
choco install graphviz
Generate PNG image:
anydi myapp:container -o dot | dot -Tpng -o graph.png
Generate SVG:
anydi myapp:container -o dot | dot -Tsvg -o graph.svg
JSON
Generate JSON for custom processing:
anydi myapp:container -o json
Output:
{
"providers": [
{
"type": "Database",
"scope": "singleton",
"from_context": false,
"dependencies": []
},
{
"type": "UserRepository",
"scope": "singleton",
"from_context": false,
"dependencies": [
{"name": "db", "type": "Database"}
]
},
{
"type": "UserService",
"scope": "singleton",
"from_context": false,
"dependencies": [
{"name": "repo", "type": "UserRepository"}
]
}
]
}
Save to file:
anydi myapp:container -o json > graph.json
Custom indentation:
anydi myapp:container -o json --indent 4 > graph.json
Options
Full Module Path
Show full module paths in output:
anydi myapp:container --full-path
Output:
myapp.services.UserService (singleton)
└── repo: myapp.repositories.UserRepository (singleton)
└── db: myapp.database.Database (singleton)
Scan Packages
Scan packages for providers before generating graph:
anydi myapp:container -s myapp
All Options
| Option | Short | Description |
|---|---|---|
--output-format |
-o |
Output format: tree, mermaid, dot, json |
--full-path |
Show full module paths | |
--indent |
-i |
JSON indentation (default: 2) |
--scan |
-s |
Packages to scan for providers |