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

zod-commander

v0.2.1

Published

A TypeScript utility for building type-safe CLI commands using commander and zod.

Readme

zod-commander

Build type-safe command-line interfaces (CLIs) from Zod schemas using Commander, with input validation, inferred action types, and generated help. zodCommand() returns a Commander Command, so all Commander methods work on the result.

Installation

npm install zod-commander zod

Usage

// cli.ts
import { z } from 'zod'
import { zodCommand } from 'zod-commander'

const greet = zodCommand({
  name: 'greet',
  summary: 'Say hello',
  description: 'Greet someone, optionally with a title and repeated greetings.',
  args: {
    name: z.string().describe('Name of the person to greet'),
  },
  opts: {
    title: z.string().optional().describe('t;Title to put before the name'),
    repeat: z.coerce.number().int().min(1).prefault(1)
      .describe('r;Number of greetings'),
    excited: z.boolean().prefault(false).describe('e;Add an exclamation mark'),
  },
  action({ name }, { title, repeat, excited }) {
    // Types are inferred: name is string, title is string | undefined,
    // repeat is number, and excited is boolean.
    const recipient = title ? `${title} ${name}` : name
    for (let i = 0; i < repeat; i++) {
      console.log(`Hello, ${recipient}${excited ? '!' : '.'}`)
    }
  },
})

await greet.parseAsync(process.argv)
node cli.ts Alice
# Hello, Alice.

node cli.ts Alice --title Dr --repeat 2 --excited
# Hello, Dr Alice!
# Hello, Dr Alice!

node cli.ts Alice -t Dr -r 2 -e
# Hello, Dr Alice!
# Hello, Dr Alice!

node cli.ts --help produces:

Usage: greet [options] <name>

Greet someone, optionally with a title and repeated greetings.

Arguments:
  name                   Name of the person to greet

Options:
  -t, --title <title>    Title to put before the name
  -r, --repeat <repeat>  Number of greetings (default: 1)
  -e, --excited          Add an exclamation mark (default: false)
  -h, --help             display help for command

How schemas map to CLI inputs

  • Arguments and options: args defines positional arguments in key order; opts defines named options. Inputs are required unless their schema accepts undefined, such as with .optional() or .prefault(). Boolean options become flags.
  • Parsing and validation: CLI values arrive as strings. Use coercion or transforms for other types, as with z.coerce.number() above. Zod validation failures become Commander input errors. Enum schemas also expose their choices in help.
  • Defaults: Use .prefault() with Zod 4. The package parses the prefault through the schema and registers the result as a Commander default. Zod 4's .default() is not extracted into a CLI default.
  • Descriptions and aliases: .describe() supplies help text. Prefix any option description with a letter and a semicolon, such as 'r;Number of greetings', to add a short alias. The prefix is removed from help.
  • Actions: action receives named argument and option objects with schema-inferred types. It can be synchronous or asynchronous.

Commander integration

zodCommand() returns a Commander Command. You can call Commander methods on it and add it to a regular Commander program. All zodCommand() properties are optional, so it can also build a root program.

Using greet from above as a subcommand, the parent program handles parsing instead of greet:

import { Command } from 'commander'

// Using the greet command defined above.
greet.alias('hello')

const program = new Command()
  .name('my-cli')
  .addCommand(greet)

await program.parseAsync(process.argv)

See the Commander documentation for command composition, parsing, and customization.

Zod 3 support

The default entry point uses Zod 4. For Zod 3, use the /zod3 entry point and .default() instead of .prefault():

import { z } from 'zod'
import { zodCommand } from 'zod-commander/zod3'