# vercel/arg architecture

> Architecture diagram of the GitHub repository vercel/arg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vercel/arg
- Repository: https://github.com/vercel/arg

## Overview

##### Main Components

a) Input Layer:
- CLI Arguments input (process.argv or custom argv)
- Specification object input
- Options configuration input (permissive, stopAtPositional)

b) Processing Core:
- Argument Parser Engine
- Type Handler System
- Alias Resolution System
- Error Handler

c) Output Layer:
- Parsed Arguments Object
- Error Objects (with codes)

##### Component Relationships

- Input → Processing → Output flow
- Clear separation between the type handling system and the main parsing logic
- Error handling flows that can exit at various points

##### Specific Implementation Details

- Type Handlers:
- Built-in (Boolean, String, Number)
- Special Handlers (COUNT, flag())
- Custom Handler Support
- Argument Processing Features:
- Alias Resolution
- Flag Processing
- Multiple Value Collection
- Positional Argument Handling

**[CLI Input] → [Specification Object] → [Options Config]**
- ↓                ↓                    ↓
- [Input Validator & Normalizer]
- ↓
- [Argument Parser Engine] ←→ [Type Handler System]
- ↓                           ↑
- [Alias Resolver]        [Built-in Types]
- ↓                    [Special Types]
- [Output Formatter]      [Custom Handlers]
- ↓
- [Parsed Result]

##### Important Details to Capture

- How the permissive mode affects the flow
- StopAtPositional behavior
- Demonstrate how multiple values are collected for array types
- Error codes (ARG_UNKNOWN_OPTION) in the error handling flow

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Input
        CLI["CLI Arguments\n(process.argv)"]
        SPEC["Specification Object"]
        OPTS["Options Configuration\n(permissive, stopAtPositional)"]
    end

    subgraph "Processing Core"
        VALID["Input Validator &\nNormalizer"]
        PARSE["Argument Parser Engine"]:::core
        ALIAS["Alias Resolver"]:::core
        
        subgraph "Type Handler System"
            TYPE["Type Handler Manager"]:::types
            BUILT["Built-in Types\n(Boolean, String, Number)"]:::types
            SPECIAL["Special Types\n(COUNT, flag())"]:::types
            CUSTOM["Custom Handler\nSupport"]:::types
        end
        
        ERR["Error Handler"]:::error
    end

    subgraph Output
        OUT["Output Formatter"]
        RESULT["Parsed Arguments\nObject"]
        ERRS["Error Objects\n(ARG_UNKNOWN_OPTION)"]:::error
    end

    %% Relationships
    CLI --> VALID
    SPEC --> VALID
    OPTS --> VALID
    
    VALID --> PARSE
    PARSE <--> TYPE
    TYPE --> BUILT
    TYPE --> SPECIAL
    TYPE --> CUSTOM
    
    PARSE --> ALIAS
    ALIAS --> OUT
    OUT --> RESULT
    
    VALID -.-> ERR
    PARSE -.-> ERR
    ALIAS -.-> ERR
    ERR --> ERRS

    %% Styles
    classDef core fill:#2374ab
    classDef types fill:#ffd700
    classDef error fill:#ff6b6b
    classDef input fill:#4CAF50
    classDef output fill:#gray

    %% Click events
    click PARSE "https://github.com/vercel/arg/blob/main/index.js"
    click TYPE "https://github.com/vercel/arg/blob/main/index.js"
    click VALID "https://github.com/vercel/arg/blob/main/index.js"
    click ALIAS "https://github.com/vercel/arg/blob/main/index.js"
    click ERR "https://github.com/vercel/arg/blob/main/test.js"
    click PARSE "https://github.com/vercel/arg/blob/main/index.d.ts"

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Type Handlers"]:::types
        L3["Error Handling"]:::error
        L4["Input Components"]:::input
        L5["Output Components"]:::output
    end
```
