# @0x-jerry/silver

> Let writing CLI like writing document.

Latest version **2.1.0** (published 2026-07-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @0x-jerry/silver
pnpm add @0x-jerry/silver
yarn add @0x-jerry/silver
bun add @0x-jerry/silver
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2026-07-17 |
| First published | 2023-06-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 5 |
| Unpacked size | 51.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 0 |
| Author | 0x-jerry |
| Maintainers | cwxyz |

## Links

- npm: https://www.npmjs.com/package/@0x-jerry/silver
- Repository: https://github.com/0x-jerry/silver
- Homepage: https://github.com/0x-jerry/silver#readme
- Issues: https://github.com/0x-jerry/silver/issues
- npm.io page: https://npm.io/package/@0x-jerry/silver

## Dependencies (5)

- [ohm-js](https://npm.io/package/ohm-js.md) ^17.5.0
- [minimist](https://npm.io/package/minimist.md) ^1.2.8
- [mustache](https://npm.io/package/mustache.md) ^4.2.0
- [picocolors](https://npm.io/package/picocolors.md) ^1.1.1
- [@0x-jerry/utils](https://npm.io/package/@0x-jerry/utils.md) ^6.5.0

## Recent versions

- 2.1.0 (latest) — 2026-07-17
- 2.0.1 — 2026-07-16
- 1.2.4 — 2025-09-10
- 1.2.3 — 2025-09-09
- 1.2.2 — 2025-09-09
- 1.2.1 — 2025-09-09
- 1.2.0 — 2025-09-09
- 1.1.1 — 2024-09-14
- 1.1.0 — 2024-09-14
- 1.0.3 — 2024-07-20
- 1.0.2 — 2024-07-20
- 1.0.1 — 2024-07-19
- 1.0.0 — 2024-07-19
- 0.1.9 — 2024-07-18
- 0.1.8 — 2024-05-18
- … 11 more at https://npm.io/package/@0x-jerry/silver/versions

## README

# Silver

Let writing CLI like writing a document.

## Install

```sh
pnpm i @0x-jerry/silver
```

## Quick Start

```ts
import { silver } from "@0x-jerry/silver";

const cli = silver`
v1.0.0 @autocomplete

my-cli [file], A CLI that writes like a document. ${mainAction}

-t --test @type:defaultValue, Test autocomplete.
-o --output @string, Output file path.

up/upgrade <dir> [...extra] #stopEarly, Upgrade something. ${upgradeAction}

  -f --force @bool, Force upgrade.
`;

// Register custom completion types
cli.type("type", () => ["value1", "value2", "value3"]);

function mainAction([file], options) {
  console.log("file:", file, "options:", options);
}

function upgradeAction([dir, ...extra], options) {
  console.log("dir:", dir, "extra:", extra, "options:", options);
}
```

## Syntax Guide

Silver uses a template literal to define your CLI. Here's the full syntax:

### Program-level

```
v1.0.0 @autocomplete @manual
```

| Token           | Description                                                |
| --------------- | ---------------------------------------------------------- |
| `v1.0.0`        | Version declaration (must be at the top)                   |
| `@autocomplete` | Enables `complete` subcommand for shell autocomplete       |
| `@manual`       | Prevents auto-execution; call `cli.execute(argv)` manually |

### Command Definition

```
commandName/alias [param] <reqParam> [...restParam] #stopEarly, Description text. ${actionFn}
```

| Part           | Example               | Description                               |
| -------------- | --------------------- | ----------------------------------------- |
| Name           | `install`             | Command name (alphanumeric + `_`)         |
| Alias          | `install/i`           | Shorthand alias via `/`                   |
| Optional param | `[name]`              | Optional positional parameter             |
| Required param | `<name>`              | Required positional parameter             |
| Rest param     | `[...items]`          | Collects remaining arguments as array     |
| Flag           | `#stopEarly`          | Command flag (see below)                  |
| Description    | `, Install packages.` | Text after comma is the description       |
| Action         | `${myFn}`             | Function to call when command is executed |

**Command flags:**

| Flag         | Description                                              |
| ------------ | -------------------------------------------------------- |
| `#stopEarly` | Stop parsing options after the first non-option argument |

### Options

```
-s --string @type, Description.
```

| Part        | Example          | Description             |
| ----------- | ---------------- | ----------------------- |
| Short name  | `-s`             | Single-letter alias     |
| Long name   | `--string`       | Full option name        |
| Type        | `@string`        | Option type (see below) |
| Description | `, Description.` | Text after comma        |

### Parameter Types

```
[@type:name]     // typed optional parameter
<@type:name>     // typed required parameter
[...@type|_files:name]  // typed rest param with file completion
```

Types are registered via `cli.type(name, getValues)`. Built-in types include `bool`, `boolean`, `number`, `string`. Special completion types `_files` and `_dirs` enable shell-native file/directory completion.

## Autocomplete

Set `@autocomplete` at the program level to enable shell completion.

### Shell Support

| Shell      | Command                                                          |
| ---------- | ---------------------------------------------------------------- |
| zsh        | `source <(your-cli complete zsh)`                                |
| PowerShell | `Invoke-Expression (your-cli complete powershell \| Out-String)` |

For persistent setup, add the output to your shell's config file (`.zshrc` or PowerShell profile).

### Custom Completion Types

```ts
cli.type("env", () => ["development", "production", "staging"]);

// Async types
cli.type("remote-branch", async () => {
  const res = await fetch("https://api.example.com/branches");
  return res.json();
});

// With descriptions (displayed in some shells)
cli.type("format", () => [
  { label: "json", desc: "JSON output" },
  { label: "yaml", desc: "YAML output" },
]);
```

### Built-in Completion Types

| Type     | Description                              |
| -------- | ---------------------------------------- |
| `_files` | File path completion (shell-native)      |
| `_dirs`  | Directory path completion (shell-native) |

These can be combined with custom types: `@custom|_files`.

## API

### `silver` (default export)

```ts
function silver(template: TemplateStringsArray, ...tokens: any[]): Silver;
```

Parses the template and optionally auto-executes with `process.argv`.

### `Silver` class

| Method                  | Description                                |
| ----------------------- | ------------------------------------------ |
| `type(name, getValues)` | Register a completion type (sync or async) |
| `execute(argv)`         | Execute the CLI with the given arguments   |
| `parse(raw, ...tokens)` | Parse a template literal programmatically  |

### Exported Types

```ts
import {
  silver,
  BuiltinType, // enum: File = '_files', Dir = '_dirs'
  type CompletionGroup, // { name: string, values: string[] }
  type CompletionOutput, // { groups: CompletionGroup[] }
  type Command,
  type CmdOption,
  type CmdParameter,
  type Program,
  type CmdAction,
  type ActionParsedArgs,
  ProgramFlag,
  CommandFlag,
} from "@0x-jerry/silver";
```

## Thanks

- [vitest](https://vitest.dev/)
- [minimist](https://github.com/minimistjs/minimist)
- [tsdown](https://tsup.egoist.dev)
- [ohm-js](https://ohmjs.org/)
- [mustache](https://github.com/janl/mustache.js)
- [picocolors](https://github.com/alexeyraspopov/picocolors)

---
_Source: https://npm.io/package/@0x-jerry/silver · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
