百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

optique

> 开发工具
开源

用于 TypeScript 的安全组合 CLI 解析器

722 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 TypeScript 的安全组合 CLI 解析器

Optique: Type-safe combinatorial CLI parser for TypeScript

Type-safe combinatorial CLI parser for TypeScript inspired by Haskell's optparse-applicative and TypeScript's Zod. Build composable parsers for command-line interfaces with full type safety, automatic type inference, and built-in shell completion support for Bash, zsh, fish, PowerShell, and Nushell, plus config file integration and man page generation from the same parser definitions.

[!NOTE] Optique is a parsing library that focuses on extracting and validating command-line arguments. It doesn't dictate your application's structure, handle command execution, or provide scaffolding—it simply transforms command-line input into well-typed data structures.

Why Optique

  • Composable by default: Build small parser pieces and combine them into larger CLIs without losing readability or types.
  • Types that model real CLI rules: Optional flags, mutually exclusive branches, and dependent options are reflected directly in inferred types.
  • One parser, many outputs: Derive help text, shell completions, and (with @optique/man) man pages from the same parser definition.
  • Practical integrations: Extend parsers with config files, environment variables, OS credential stores, schema validators, interactive prompts, and git-aware parsing.
  • Command discovery: Split larger command trees into files with @optique/discover while keeping parser-driven help and completion.
  • Cross-runtime consistency: Use the same parser model in Deno, Node.js, and Bun.

Features

  • Parser combinators: object(), or(), merge(), optional(), multiple(), map(), conditional(), passThrough(), and more for composable CLI parsing
  • Full type safety: Automatic TypeScript type inference for all parser compositions with compile-time validation
  • Rich value parsers: Built-in parsers for strings, numbers, URLs, locales, UUIDs, networking types (port(), ipv4(), hostname(), email(), etc.), Temporal types (via @optique/temporal), Standard Schema validators (via @optique/standard-schema), Zod schemas (via @optique/zod), and Valibot schemas (via @optique/valibot)
  • Config file support: Load config from files with Standard Schema validation (via @optique/config), supporting Zod, Valibot, ArkType, and more
  • Environment variable support: Bind options to environment variables with type-safe parsing and fallback behavior (via @optique/env)
  • OS credential-store support: Fill missing password options from the OS credential store with async fallback behavior (via @optique/keyring)
  • Derived defaults: Compute default values from the first-pass parse result without lowering CLI argument priority (via @optique/derived-defaults)
  • Interactive prompts: Prompt users for missing values via Inquirer.js or Clack with parser-integrated fallback flows (via @optique/inquirer and @optique/clack)
  • Inter-option dependencies: Options whose valid values depend on other options, with dynamic validation and context-aware shell completion
  • Async parser support: Type-safe sync/async mode distinction for parsers that validate against external sources like git refs or remote APIs
  • Man page generation: Generate Unix man pages directly from parser definitions (via @optique/man), keeping documentation always in sync
  • Command discovery: Discover command modules from a directory and dispatch to type-checked handlers (via @optique/discover)
  • Shell completion: Automatic completion script generation for Bash, zsh, fish, PowerShell, and Nushell
  • Smart error messages: “Did you mean?” suggestions for typos with context-aware error formatting
  • Automatic help generation: Beautiful help text with usage formatting, labeled sections, and colored output
  • Multi-runtime support: Works seamlessly with Deno, Node.js, and Bun
  • CLI integration: Complete CLI setup with run() function including help, version, and completion support

Quick example

import { option, constant } from "@optique/core/primitives";
import { object, or, merge } from "@optique/core/constructs";
import { optional } from "@optique/core/modifiers";
import { string, integer } from "@optique/core/valueparser";
import { run, print } from "@optique/run";

// Reusable parser components
const commonOptions = object({
  verbose: option("-v", "--verbose"),
  config: optional(option("-c", "--config", string())),
});

// Mutually exclusive deployment strategies
const localDeploy = object({
  mode: constant("local" as const),
  path: option("--path", string()),
  port: option("--port", integer({ min: 1000 })),
});

const cloudDeploy = object({
  mode: constant("cloud" as const),
  provider: option("--provider", string()),
  region: option("--region", string()),
  apiKey: option("--api-key", string()),
});

// Compose parsers with type-safe constraints
const parser = merge(
  commonOptions,
  or(localDeploy, cloudDeploy)
);

const config = run(parser, { help: "both" });
// config: {
//   readonly verbose: boolean;
//   readonly config: string | undefined;
// } & (
//   | {
//       readonly mode: "local";
//       readonly path: string;
//       readonly port: number;
//   }
//   | {
//       readonly mode: "cloud";
//       readonly provider: string;
//       readonly region: string;
//       readonly apiKey: string;
//   }
// )

// TypeScript knows exactly what's available based on the mode
if (config.mode === "local") {
  print(`Deploying to ${config.path} on port ${config.port}.`);
} else {
  print(`Deploying to ${config.provider} in ${config.region}.`);
}

Docs

Optique provides comprehensive documentation to help you get started quickly: .

New to Optique? Start with the tutorial and then explore the cookbook.

  • Why Optique? — What makes Optique different from other CLI libraries
  • Tutorial — Step-by-step guide from simple options to nested subcommands
  • Cookbook — Practical recipes for common CLI patterns including shell completion

API reference documentation for each package is available on JSR (see below).

Packages

Optique is a monorepo which contains multiple packages. The main package is @optique/core, which provides the shared types and parser combinators. The following is a list of the available packages:

Package JSR npm Description
@optique/core JSR npm Shared types and parser combinators
@optique/run JSR npm Runner for Node.js/Deno/Bun
@optique/discover JSR npm Runtime-aware command discovery
@optique/config JSR npm Config file support with Standard Schema
@optique/clack JSR npm Clack prompt support
@optique/derived-defaults JSR npm Defaults derived from parsed values
@optique/env [JSR][jsr:@optique/env] [npm][npm:@optique/env] Environment variable integration
@optique/keyring [JSR][jsr:@optique/keyring] [npm][npm:@optique/keyring] OS credential-store password fallback
@optique/git [JSR][jsr:@optique/git] [npm][npm:@optique/git] Git reference parsers (branches, tags, etc)
@optique/logtape [JSR][jsr:@optique/logtape] [npm][npm:@optique/logtape] [LogTape] logging integration
@optique/man [JSR][jsr:@optique/man] [npm][npm:@optique/man] Man page generation from parsers
@optique/standard-schema [JSR][jsr:@optique/standard-schema] [npm][npm:@optique/standard-schema] Standard Schema value parser integration
@optique/temporal [JSR][jsr:@optique/temporal] [npm][npm:@optique/temporal] [Temporal] value parsers (date and time)
@optique/valibot [JSR][jsr:@optique/valibot] [npm][npm:@optique/valibot] [Valibot] schema integration for validation
@optique/zod [JSR][jsr:@optique/zod] [npm][npm:@optique/zod] Zod schema integration for validation
@optique/inquirer [JSR][jsr:@optique/inquirer] [npm][npm:@optique/inquirer] [Inquirer.js] prompt support
@optique/prompt [JSR][jsr:@optique/prompt] [npm][npm:@optique/prompt] Generic prompt adapter foundation
@optique/testing [JSR][jsr:@optique/testing] [npm][npm:@optique/testing] Parser, runner, and subprocess CLI tests

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptcligetoptparser-combinatorstypescript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具