| description | Discussion of the CLI commands and options part of the codebase |
|---|
This document covers the command-line interface implementation, including command structure, argument parsing, validation, and extension patterns.
The CLI is built using a modular command structure where each command is implemented as a separate module with standardized interfaces for argument parsing, validation, and execution.
- Command Registration: Commands are registered through a central registry system
- Argument Parsing: Uses a consistent parsing framework for options, flags, and positional arguments
- Validation Pipeline: Multi-stage validation including syntax, semantic, and context validation
- Error Handling: Standardized error reporting with user-friendly messages and exit codes
Commands follow a consistent structure with:
- Command metadata (name, description, aliases)
- Argument/option definitions with types and validation rules
- Handler function for command execution
- Help text generation
- Boolean flags: Simple on/off switches
- String options: Text input with optional validation patterns
- Enum options: Predefined value sets with validation
- Array options: Multiple values of the same type
- File/path options: Special handling for filesystem references
- Create command module in appropriate subdirectory
- Define command schema with full type information
- Implement validation logic (both sync and async where needed)
- Add comprehensive error handling with specific error codes
- Include help text and examples
- Register command in central registry
- Add integration tests covering common usage patterns
- Use kebab-case for multi-word options (
--config-file) - Provide short aliases for frequently used options (
-cfor--config) - Boolean flags should be positive by default (
--enable-featurenot--disable-feature) - Use consistent naming across related commands
- Input Validation: Check argument types, ranges, and format requirements
- Context Validation: Verify prerequisites, file existence, permissions
- Cross-option Validation: Ensure option combinations are valid
- Async Validation: Handle network-dependent or filesystem validation
Commands integrate with the configuration system to:
- Load default values from config files
- Override config with command-line arguments
- Validate configuration consistency
- Use structured logging for debugging and audit trails
- Implement consistent output formatting (JSON, table, plain text)
- Handle progress reporting for long-running operations
- Map internal errors to user-friendly messages
- Use specific exit codes for different error categories
- Provide actionable error messages with suggested fixes
Most commands involve async operations (file I/O, network requests). Follow patterns for:
- Proper async/await usage
- Timeout handling
- Graceful cancellation
- Progress reporting
- Always validate paths before operations
- Handle relative vs absolute path resolution
- Implement proper error handling for permissions, missing files
- Consider cross-platform path handling
Commands often need to merge configuration from multiple sources:
- Default values
- Configuration files
- Environment variables
- Command-line arguments
- Test command parsing in isolation
- Validate option validation logic
- Mock external dependencies
- Test error conditions and edge cases
- Test complete command execution flows
- Verify file system interactions
- Test configuration loading and merging
- Validate output formatting
- Be careful with optional vs required arguments
- Handle edge cases in string parsing (quotes, escaping)
- Validate mutually exclusive options
- Consider default value precedence
- Avoid technical jargon in user-facing messages
- Provide specific error locations (line numbers, file paths)
- Include suggested fixes when possible
- Use consistent error formatting
- Lazy-load command modules to improve startup time
- Cache validation results when appropriate
- Optimize for common usage patterns
- Handle large input sets efficiently
The validation system supports custom validators for domain-specific requirements.
New output formats can be added through the formatter registry.
External commands can be registered through the plugin system.
/src/spec-node/devContainersSpecCLI.ts- Main CLI entry point/src/spec-configuration/- Configuration parsing and validation/src/spec-utils/- Shared utilities for command implementation- Tests in
/src/test/following command structure