npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@guanghechen/commander

v4.5.1

Published

A minimal, type-safe command-line interface builder with fluent API

Downloads

2,562

Readme

A minimal, type-safe command-line interface builder with fluent API. Supports subcommands, option parsing, shell completion generation (bash, fish, pwsh), and built-in help/version handling.

Install

  • npm

    npm install --save @guanghechen/commander
  • yarn

    yarn add @guanghechen/commander

Usage

Basic Command

import { Command } from '@guanghechen/commander'

const cli = new Command({
  name: 'mycli',
  version: '1.0.0',
  description: 'My awesome CLI tool',
})

cli
  .option({
    long: 'verbose',
    short: 'v',
    type: 'boolean',
    description: 'Enable verbose output',
  })
  .option({
    long: 'output',
    short: 'o',
    type: 'string',
    description: 'Output file path',
    default: './output.txt',
  })
  .argument({
    name: 'file',
    kind: 'required',
    description: 'Input file to process',
  })
  .action(({ opts, args, ctx }) => {
    const [file] = args
    ctx.reporter.info(`Processing ${file}...`)
    if (opts['verbose']) {
      ctx.reporter.debug(`Output: ${opts['output']}`)
    }
  })

cli.run({
  argv: process.argv.slice(2),
  envs: process.env,
})

Subcommands

import { Command } from '@guanghechen/commander'

const root = new Command({
  name: 'git',
  version: '1.0.0',
  description: 'A simple git-like CLI',
})

const clone = new Command({
  description: 'Clone a repository',
})
  .argument({ name: 'url', kind: 'required', description: 'Repository URL' })
  .option({ long: 'depth', type: 'number', description: 'Shallow clone depth' })
  .action(({ args, opts }) => {
    console.log(`Cloning ${args[0]} with depth ${opts['depth'] ?? 'full'}`)
  })

const commit = new Command({
  description: 'Record changes to the repository',
})
  .option({ long: 'message', short: 'm', type: 'string', required: true, description: 'Commit message' })
  .option({ long: 'amend', type: 'boolean', description: 'Amend previous commit' })
  .action(({ opts }) => {
    console.log(`Committing: ${opts['message']}`)
  })

root.subcommand('clone', clone).subcommand('commit', commit).subcommand('ci', commit)

root.run({ argv: process.argv.slice(2), envs: process.env })

Shell Completion

import { Command, CompletionCommand } from '@guanghechen/commander'

const root = new Command({
  name: 'mycli',
  version: '1.0.0',
  description: 'My CLI with completion support',
})

// Add completion subcommand
root.subcommand('completion', new CompletionCommand(root))

// Generate completion scripts:
// mycli completion --bash > ~/.local/share/bash-completion/completions/mycli
// mycli completion --fish > ~/.config/fish/completions/mycli.fish
// mycli completion --pwsh >> $PROFILE

Option Types

import { Command } from '@guanghechen/commander'

new Command({ name: 'example', description: 'Option types demo' })
  // Boolean (flags)
  .option({ long: 'debug', type: 'boolean', description: 'Enable debug mode' })

  // String with choices
  .option({
    long: 'format',
    type: 'string',
    choices: ['json', 'yaml', 'toml'],
    default: 'json',
    description: 'Output format'
  })

  // Number
  .option({ long: 'port', type: 'number', default: 3000, description: 'Server port' })

  // Array (can be specified multiple times)
  .option({ long: 'include', type: 'string[]', description: 'Files to include' })

  // Required option
  .option({ long: 'config', type: 'string', required: true, description: 'Config file' })

  // Custom coercion
  .option({
    long: 'date',
    type: 'string',
    coerce: (value) => new Date(value),
    description: 'Date value',
  })

Built-in Coerce Factories

import { Coerce, Command } from '@guanghechen/commander'

new Command({ name: 'example', desc: 'Coerce demo' })
  .option({
    long: 'offset',
    type: 'number',
    args: 'required',
    coerce: Coerce.integer('--offset'),
    desc: 'Signed offset',
  })
  .option({
    long: 'parallel',
    type: 'number',
    args: 'required',
    coerce: Coerce.positiveInteger('--parallel'),
    desc: 'Parallel workers',
  })
  .option({
    long: 'duration',
    type: 'number',
    args: 'required',
    coerce: Coerce.positiveNumber('--duration'),
    desc: 'Duration in seconds',
  })
  .option({
    long: 'port',
    type: 'number',
    args: 'required',
    coerce: Coerce.port('--port'),
    desc: 'Server port',
  })
  .option({
    long: 'domain',
    type: 'string',
    args: 'required',
    coerce: Coerce.domain('--domain'),
    desc: 'Domain name',
  })
  .option({
    long: 'ip',
    type: 'string',
    args: 'required',
    coerce: Coerce.ip('--ip'),
    desc: 'IP address',
  })
  .option({
    long: 'host',
    type: 'string',
    args: 'required',
    coerce: Coerce.host('--host'),
    desc: 'Host (IP or domain)',
  })
  .option({
    long: 'mode',
    type: 'string',
    args: 'required',
    coerce: Coerce.choice('--mode', ['dev', 'test', 'prod'] as const),
    desc: 'Deploy mode',
  })
  .option({
    long: 'scale',
    type: 'number',
    args: 'required',
    coerce: Coerce.number('--scale'),
    desc: 'Scale factor',
  })

Default error message format:

{name} is expected as {coerce type}, but got {raw}

You can still override the message via Coerce.xxx(name, 'custom error message').

Built-in Is Helpers

import { isDomain, isIp, isIpv4, isIpv6 } from '@guanghechen/commander'

isIpv4('127.0.0.1') // true
isIpv6('::1') // true
isIp('2001:db8::1') // true
isDomain('example.com') // true

Help Examples

import { Command } from '@guanghechen/commander'

const cli = new Command({ name: 'mycli', desc: 'My CLI tool' })

cli
  .example('Initialize Project', 'init my-app', 'Create project scaffold')
  .example('Watch Build', 'build --watch', 'Rebuild on file changes')
  .action(() => {})

await cli.run({ argv: ['--help'], envs: process.env })

usage 是相对当前 command path 的片段,help 中会自动补齐前缀,例如 mycli build --watch

--color / --no-color 仅控制 help 文本的终端着色; --log-colorful / --no-log-colorful 控制 Reporter 的日志着色。

当环境变量 NO_COLOR 存在时,help 渲染默认视为 --no-color;显式传入 --color 可以覆盖这个默认值。

Reference