CLI - Command-Line Interface¶
The grai.build CLI provides a Typer-based command-line interface for working with knowledge graph projects.
Overview¶
The CLI exposes five main commands:
grai init- Initialize a new projectgrai validate- Validate entity and relation definitionsgrai build- Build project by compiling to Cyphergrai compile- Compile without validationgrai info- Show project information and statistics
Installation¶
Or install from source:
Commands¶
grai init¶
Initialize a new grai.build project with example entities and relations.
Usage:
Arguments:
PATH- Directory to initialize project in (default:.)
Options:
--name,-nTEXT - Project name (default:my-knowledge-graph)--force,-f- Overwrite existing files--help- Show help message
Examples:
# Initialize in current directory
grai init
# Initialize in specific directory with custom name
grai init my-project --name my-graph
# Overwrite existing project
grai init my-project --force
Created Structure:
my-project/
├── grai.yml # Project configuration
├── README.md # Project documentation
├── entities/ # Entity definitions
│ ├── customer.yml
│ └── product.yml
├── relations/ # Relation definitions
│ └── purchased.yml
└── target/ # Compiled output
└── neo4j/
└── compiled.cypher
grai validate¶
Validate entity and relation definitions for correctness and consistency.
Usage:
Arguments:
PROJECT_DIR- Path to grai.build project directory (default:.)
Options:
--strict,-s- Treat warnings as errors--verbose,-v- Show detailed validation output--help- Show help message
Examples:
# Validate current directory
grai validate
# Validate specific project
grai validate my-project
# Strict mode (warnings as errors)
grai validate --strict
# Verbose output
grai validate --verbose
Validation Checks:
- ✅ Entity references exist
- ✅ Key mappings are valid
- ✅ No duplicate property names
- ✅ No duplicate entity/relation names
- ✅ No circular dependencies
- ✅ Required fields are present
Output:
🔍 Validating project...
✓ Loaded project: my-graph (v1.0.0)
- 2 entities
- 1 relations
✓ Validation passed!
grai build¶
Build the project by validating and compiling to Neo4j Cypher.
Usage:
Arguments:
PROJECT_DIR- Path to grai.build project directory (default:.)
Options:
--output,-oPATH - Output directory (default:target/neo4j)--filename,-fTEXT - Output filename (default:compiled.cypher)--schema-only- Generate only schema (constraints and indexes)--skip-validation- Skip validation before compiling--verbose,-v- Show detailed build output--help- Show help message
Examples:
# Build current directory
grai build
# Build with custom output
grai build --output dist/cypher --filename my-graph.cypher
# Build schema only
grai build --schema-only
# Build without validation
grai build --skip-validation
# Verbose output with build summary
grai build --verbose
Output:
🔨 Building project...
✓ Loaded project: my-graph (v1.0.0)
→ Validating...
✓ Validation passed
→ Compiling to Cypher...
✓ Compiled successfully
✓ Wrote output to: target/neo4j/compiled.cypher
✓ Build complete!
Build Summary
┏━━━━━━━━━━━━━┳━━━━━━━┓
┃ Metric ┃ Count ┃
┡━━━━━━━━━━━━━╇━━━━━━━┩
│ Entities │ 2 │
│ Relations │ 1 │
│ Constraints │ 2 │
│ Indexes │ 8 │
│ Statements │ 3 │
└─────────────┴───────┘
grai compile¶
Compile project to Cypher without validation. This is an alias for grai build --skip-validation.
Usage:
Arguments:
PROJECT_DIR- Path to grai.build project directory (default:.)
Options:
--output,-oPATH - Output directory (default:target/neo4j)--help- Show help message
Examples:
# Compile without validation
grai compile
# Compile to custom directory
grai compile --output dist/cypher
Use Case:
Use compile when you've already validated and just want to regenerate the Cypher output. Use build for the full validation + compilation workflow.
grai info¶
Show project information and statistics.
Usage:
Arguments:
PROJECT_DIR- Path to grai.build project directory (default:.)
Options:
--help- Show help message
Examples:
Output:
📊 Project Information
Project: my-graph
┌──────────────────────┬───────┐
│ Name │ my-graph │
│ Version │ 1.0.0 │
│ Entities │ 2 │
│ Relations │ 1 │
│ Entity Properties │ 10 │
│ Relation Properties │ 4 │
└──────────────────────┴───────┘
Entities
┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Entity ┃ Source ┃ Keys ┃ Properties ┃
┡━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━┩
│ customer │ analytics.customers │ cust_id │ 5 │
│ product │ analytics.products │ prod_id │ 5 │
└──────────┴─────────────────────┴─────────┴────────────┘
Relations
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Relation ┃ From → To ┃ Source ┃ Properties ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ PURCHASED │ customer → product │ analytics.orders │ 4 │
└───────────┴────────────────────┴──────────────────┴────────────┘
Global Options¶
All commands support these global options:
--version,-v- Show version and exit--help- Show help message and exit
Examples:
# Show version
grai --version
# Show general help
grai --help
# Show help for specific command
grai build --help
Complete Workflows¶
Basic Workflow¶
# 1. Initialize project
grai init my-graph-project --name my-graph
# 2. Edit definitions
cd my-graph-project
vim entities/customer.yml
vim relations/purchased.yml
# 3. Validate
grai validate
# 4. Build
grai build
# 5. View output
cat target/neo4j/compiled.cypher
Development Workflow¶
# Initialize
grai init
# Iterate: edit, validate, build
vim entities/new-entity.yml
grai validate
grai build --verbose
# Check project stats
grai info
Production Workflow¶
# Validate in CI/CD
grai validate --strict
# Build for deployment
grai build --output dist/cypher --filename production.cypher
# Schema-only for database initialization
grai build --schema-only --filename schema.cypher
Error Handling¶
The CLI provides clear error messages with exit codes:
0- Success1- Error (validation failure, file not found, etc.)
Example Errors:
$ grai validate
✗ Validation failed!
Errors:
✗ Relation 'PURCHASED' references unknown entity 'order'
✗ Entity 'customer' has duplicate property 'email'
Rich Output¶
The CLI uses Rich for beautiful, colored terminal output:
- ✓ Green checkmarks for success
- ✗ Red X marks for errors
- ⚠ Yellow warnings
- → Cyan arrows for progress
- 📊 Tables for structured data
- 🚀 Emoji for visual context
Environment Variables¶
Currently, grai.build doesn't use environment variables, but future versions may support:
GRAI_HOME- Default project directoryGRAI_OUTPUT_DIR- Default output directoryGRAI_NEO4J_URI- Default Neo4j connection URI
Shell Completion¶
Shell completion is currently disabled but may be added in future versions.
API¶
The CLI can also be used programmatically:
from typer.testing import CliRunner
from grai.cli.main import app
runner = CliRunner()
result = runner.invoke(app, ["validate", "my-project"])
if result.exit_code == 0:
print("Validation passed!")
else:
print(f"Error: {result.stdout}")
See Also¶
- Parser Documentation - YAML parsing details
- Validator Documentation - Validation rules
- Compiler Documentation - Cypher generation
- Project Progress - Development status
Contributing¶
To add new CLI commands:
- Add command function to
grai/cli/main.py - Decorate with
@app.command() - Add tests to
tests/test_cli.py - Update this documentation
Example:
@app.command()
def my_command(
arg: str = typer.Argument(..., help="My argument"),
option: bool = typer.Option(False, "--option", "-o", help="My option"),
):
"""My command description."""
console.print(f"Running my command with {arg}")
# Implementation here
Troubleshooting¶
Command not found¶
If grai command is not found after installation:
Import errors¶
If you see import errors:
# Install dependencies
pip install pydantic pyyaml typer rich neo4j
# Or install with dev dependencies
pip install -e ".[dev]"
Permission errors¶
If you get permission errors when writing files:
# Check directory permissions
ls -la
# Use sudo (not recommended for user projects)
sudo grai build
# Or change output directory
grai build --output ~/my-graphs
Version: 0.1.0 Last Updated: January 2025