CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-awscli

Universal Command Line Environment for AWS.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

command-system.mddocs/

Command System

Comprehensive command framework for creating and executing AWS service operations and custom commands. The command system provides a hierarchical structure that supports AWS services, operations, and custom commands with argument processing and help generation.

Capabilities

Base Command Interface

The foundational interface that all CLI commands must implement, providing the basic contract for command execution and help generation.

class CLICommand:
    @property
    def name(self) -> str:
        """Command name."""
    
    @property
    def lineage(self) -> list:
        """Command hierarchy lineage."""
    
    @property
    def lineage_names(self) -> list[str]:
        """List of command names in hierarchy."""
    
    @property
    def arg_table(self) -> dict:
        """Argument table for command parameters."""
    
    def __call__(self, args, parsed_globals):
        """
        Execute the command.
        
        Parameters:
            args: parsed command arguments
            parsed_globals: parsed global arguments
            
        Returns:
            Command execution result
        """
    
    def create_help_command(self):
        """
        Create help command for this command.
        
        Returns:
            Help command instance
        """

Service Command Implementation

Concrete implementation for AWS service commands that handles service-level operations and subcommand routing.

class ServiceCommand(CLICommand):
    def __init__(self, cli_name, session, service_name=None):
        """
        Initialize service command for AWS service.
        
        Parameters:
            cli_name: str, CLI name for the service (e.g., 's3', 'ec2')
            session: botocore.session.Session, AWS session
            service_name: str, optional service name (defaults to cli_name)
        """
    
    @property
    def service_model(self):
        """Botocore service model providing service metadata and operations."""
    
    def create_help_command(self):
        """Create service-level help command showing available operations."""

Usage Example:

import botocore.session
from awscli.clidriver import ServiceCommand

# Create service command for S3
session = botocore.session.Session()
s3_command = ServiceCommand('s3', session)

# Access service information
print(f"Service name: {s3_command.name}")
print(f"Available operations: {s3_command.service_model.operation_names}")

Service Operation Implementation

Represents individual operations within AWS services, handling operation-specific argument processing and execution.

class ServiceOperation:
    def __init__(self, name, parent_name, operation_caller, operation_model, session):
        """
        Initialize service operation.
        
        Parameters:
            name: str, operation name (e.g., 'describe-instances')
            parent_name: str, parent service name (e.g., 'ec2')
            operation_caller: CLIOperationCaller, handles operation execution
            operation_model: botocore operation model
            session: botocore.session.Session, AWS session
        """
    
    @property
    def name(self) -> str:
        """Operation name."""
    
    @property
    def lineage(self) -> list:
        """Full command hierarchy including service and operation."""
    
    @property
    def lineage_names(self) -> list[str]:
        """List of command names from root to this operation."""
    
    @property
    def arg_table(self) -> dict:
        """
        Argument table mapping parameter names to argument definitions.
        Contains all operation parameters as CLI arguments.
        """
    
    def create_help_command(self):
        """Create operation-level help command with parameter documentation."""

Usage Example:

from awscli.clidriver import ServiceOperation, CLIOperationCaller
import botocore.session

# Create session and operation caller
session = botocore.session.Session()
operation_caller = CLIOperationCaller(session)

# Get operation model
ec2_model = session.get_service_model('ec2')
describe_instances_model = ec2_model.operation_model('DescribeInstances')

# Create operation
operation = ServiceOperation(
    name='describe-instances',
    parent_name='ec2',
    operation_caller=operation_caller,
    operation_model=describe_instances_model,
    session=session
)

# Access operation information
print(f"Operation: {operation.name}")
print(f"Lineage: {operation.lineage_names}")
print(f"Available parameters: {list(operation.arg_table.keys())}")

Command Hierarchy Structure

The AWS CLI uses a hierarchical command structure:

aws (root)
├── s3 (service)
│   ├── ls (operation)
│   ├── cp (operation)
│   └── sync (operation)
├── ec2 (service)
│   ├── describe-instances (operation)
│   ├── run-instances (operation)
│   └── terminate-instances (operation)
└── iam (service)
    ├── list-users (operation)
    ├── create-user (operation)
    └── delete-user (operation)

Command Registration

Commands are dynamically registered based on AWS service models:

# Service commands are created from botocore service models
service_names = session.get_available_services()
for service_name in service_names:
    service_command = ServiceCommand(service_name, session)
    
    # Operations are created from service model operations
    service_model = session.get_service_model(service_name)
    for operation_name in service_model.operation_names:
        operation = ServiceOperation(
            name=operation_name,
            parent_name=service_name,
            operation_caller=operation_caller,
            operation_model=service_model.operation_model(operation_name),
            session=session
        )

Advanced Command Patterns

Command Execution Flow

  1. Command Parsing: Arguments are parsed into command hierarchy
  2. Command Resolution: Appropriate command instance is located
  3. Argument Processing: Command arguments are validated and processed
  4. Command Execution: Command's __call__ method is invoked
  5. Response Formatting: Output is formatted according to specified format

Custom Command Integration

While service commands are automatically generated, custom commands can be integrated:

from awscli.commands import CLICommand

class CustomServiceCommand(CLICommand):
    def __init__(self, session):
        self._session = session
        self._name = 'custom-service'
    
    @property
    def name(self):
        return self._name
    
    @property
    def lineage(self):
        return [self]
    
    @property
    def lineage_names(self):
        return [self.name]
    
    @property
    def arg_table(self):
        return {
            'param1': CustomArgument('param1', help_text='Custom parameter'),
        }
    
    def __call__(self, args, parsed_globals):
        print(f"Executing custom service with args: {args}")
        return 0
    
    def create_help_command(self):
        return CustomHelpCommand(self)

Error Handling in Commands

Commands should handle errors gracefully and return appropriate exit codes:

class RobustCommand(CLICommand):
    def __call__(self, args, parsed_globals):
        try:
            # Command logic here
            return 0  # Success
        except ValidationError as e:
            sys.stderr.write(f"Validation error: {e}\n")
            return 1  # Validation failure
        except ServiceError as e:
            sys.stderr.write(f"Service error: {e}\n")
            return 2  # Service failure
        except Exception as e:
            sys.stderr.write(f"Unexpected error: {e}\n")
            return 255  # Unexpected failure

Install with Tessl CLI

npx tessl i tessl/pypi-awscli

docs

argument-processing.md

command-system.md

core-driver.md

custom-commands.md

error-handling.md

help-system.md

index.md

output-formatting.md

plugin-system.md

testing-framework.md

utilities.md

tile.json