utf8-forge 0.1.0

A focused, idempotent, bidirectional charset transcoder for source trees. Default output: UTF-8 without BOM.
Documentation
# docs/USAGE.md (English)

# utf8-forge — Full Usage Guide

> Complete CLI reference, recipes, and operational notes.
> For architecture and invariants see [DESIGN.md]../DESIGN.md.
> For the `D:\server` rollout see [EXECUTION.md]../EXECUTION.md.

---

## 1. Synopsis

```
utf8-forge <COMMAND> [OPTIONS]
```

Commands: `scan`, `convert`, `verify`.

---

## 2. Commands

### 2.1 `scan`

Detect encodings only. Writes nothing. Emits a summary + optional JSON report.

```bash
utf8-forge scan --root D:\server --report scan.json
```

Exit codes:

- `0` — all files classified.
- `1` — `unknown > 0`.

### 2.2 `convert`

Detect + transcode + atomic write.

```bash
# Default: → UTF-8 without BOM
utf8-forge convert --root D:\server

# Dry-run
utf8-forge convert --root D:\server --dry-run --report dry.json

# With backup
utf8-forge convert --root D:\server --backup-dir D:\backup\utf8-forge

# Custom target
utf8-forge convert --root D:\server --target gb18030

# Remote file
utf8-forge convert --url https://example.com/a.cpp --output ./a.cpp
```

Exit codes:

- `0` — success (no unexpected errors).
- `1` — some files `unknown` / `decode_error` / `io_error`.
- `2` — fatal (bad args, missing root).

### 2.3 `verify`

Assert every whitelisted file is UTF-8 without BOM.

```bash
utf8-forge verify --root D:\server --report verify.json
```

Exit codes:

- `0` — all UTF-8 no BOM.
- `3` — one or more files not UTF-8 no BOM.

---

## 3. Options

| Flag                       | Default         | Notes                                          |
|----------------------------|-----------------|------------------------------------------------|
| `-r, --root <PATH>`        | `.`             | Recursive root                                 |
| `-e, --ext <LIST>`         | see DESIGN §5.1 | **Overrides** default whitelist                |
| `-x, --exclude <LIST>`     | see DESIGN §5.2 | Appended to default excludes                   |
| `-X, --exclude-ext <LIST>` | empty           | Appended                                       |
| `--exclude-glob <GLOB>`    | empty           | Repeatable                                     |
| `--include-rc`             | false           | Adds `.rc/.rc2/.dlg`                           |
| `--include-msbuild`        | false           | Adds `.sln/.vcxproj/...`                       |
| `--include-hidden`         | false           | Processes dot-files                            |
| `-t, --target <ENC>`       | `utf-8`         | `utf-8-bom`, `gb18030`, `utf-16le`, `utf-16be` |
| `--dry-run`                | false           | No writes                                      |
| `--backup-dir <PATH>`      | none            | Backup before overwrite                        |
| `--max-file-size <BYTES>`  | 200 MiB         | Skip oversized                                 |
| `--threads <N>`            | CPU count       | Rayon pool                                     |
| `--report <PATH>`          | none            | JSON report                                    |
| `--url <URL>`              | none            | **Remote fetch (convert only)**                |
| `--output <PATH>`          | derived         | Destination for `--url`                        |
| `-q, --quiet`              | false           | Errors only                                    |
| `-v, --verbose`            | false           | Per-file detail                                |

Conflicts: `--include-rc` + `--exclude-ext rc` → fatal (exit 2).

---

## 4. Encoding Semantics

### 4.1 Detection order

UTF-8 BOM → UTF-16 LE BOM → UTF-16 BE BOM → strict UTF-8 → strict GB18030 → `Unknown`.

### 4.2 BOM policy

- Default output has **no BOM**.
- `--target utf-8-bom` adds `EF BB BF`.
- `--target utf-16le/be` emits `FF FE` / `FE FF` manually (encoding_rs does not).
- Input BOM is stripped during decode.

### 4.3 Line endings

Never normalized. CRLF stays CRLF; LF stays LF.

---

## 5. Recipes

### 5.1 C/C++ only

```bash
utf8-forge convert --root D:\server --ext h,hpp,c,cpp,cc,inl
```

### 5.2 Include MSBuild

```bash
utf8-forge convert --root D:\server --include-msbuild
```

### 5.3 Exclude a module

```bash
utf8-forge scan --root D:\server --exclude "GameServer201,Test_Client"
```

### 5.4 Glob exclusion

```bash
utf8-forge convert --root D:\server --exclude-glob "**/Generated/**"
```

### 5.5 Remote GBK → UTF-8

```bash
utf8-forge convert \
  --url https://example.com/legacy_server.cpp \
  --output D:\server\external\legacy_server.cpp \
  --target utf-8 \
  --report fetch-report.json
```

### 5.6 Reverse (legacy only)

```bash
utf8-forge convert --root D:\server\legacy\scripts --target gb18030 --ext bat,cmd
```

---

## 6. Reports

JSON report schema is documented in `EXECUTION.md §12`.

Key fields:

- `summary.total_files` — every file visited.
- `summary.candidates` — files matching whitelist.
- `summary.already_utf8` / `converted` / `unknown` / `decode_errors` / `io_errors` / `too_large` / `fetch_error`.
- `files[].action` — `would_convert` | `converted` | `unchanged` | `skipped_unknown` | `error`.

---

## 7. Exit Codes

| Code | Meaning                                                           |
|------|-------------------------------------------------------------------|
| 0    | Success                                                           |
| 1    | Non-fatal per-file issues (`unknown`, `decode_error`, `io_error`) |
| 2    | Fatal (bad args, missing root, I/O setup failure)                 |
| 3    | `verify` found non-UTF-8 whitelisted files                        |

---

## 8. CI Integration

```yaml
- name: Verify encoding
  run: utf8-forge verify --root . --report encoding-report.json
```

`verify` exit 3 fails the job — this is the intended gate.

---

## 9. Troubleshooting

See `AGENT.md §9` and `EXECUTION.md §11`.