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

zodcli

v0.0.4

Published

A CLI parser built with Zod.

Readme

zodcli

A CLI parser built with Zod.

Getting Started

npm install --save zodcli zod
import { z } from "zod";
import { argumentParser } from "zodcli";

argumentParser({ options: z.object({ foo: z.string() }).strict() }).parse([
	"--foo=bar",
]);

Using in a CLI

You can get CLI arguments passed to the script by using process.argv.slice(2):

// ./cli.js

import { z } from "zod";
import { argumentParser } from "./dist/index.js";

console.log(
	argumentParser({ options: z.object({ foo: z.string() }).strict() }).parse(
		process.argv.slice(2)
	)
);

// $ node cli.js --foo=bar
// { foo: 'bar' }

API

argumentParser

argumentParser is a function which takes an object with the following properties: options and aliases.

options

options is a required property and must have a value of a strict Zod object (z.object({}).strict()). This ensures that any unrecognized arguments are rejected.

  • All properties must be in 'camelCase' and be exclusively composed of a-z characters. For example:

    • Valid:
      • foo
      • catDog
    • Invalid:
      • Foo
      • cat_dog
      • 5
      • a5

    When parsed, '--kebab-case' arguments are converted to their respective 'camelCase' properties.

    For example:

    import { z } from "zod";
    import { argumentParser } from "zodcli";
    
    const options = z
    	.object({
    		foo: z.string(),
    		catDog: z.string(),
    	})
    	.strict();
    
    const result = argumentParser({
    	options,
    }).parse(["--foo=bar", "--cat-dog=fish"]);
    
    console.log(result);
    // { foo: 'bar', catDog: 'fish' }
  • All values must be one of the following Zod types:

    And these values can be modified/wrapped with any of the following:

aliases

aliases is an optional property which allows you to configure argument aliases for options. It is an object where: the properties are lowercase and composed of exclusively a-z charaters; and the values are 'camelCase' strings and appear as properties in the options object.

Tips and Tricks

Parsing

Parsing requires an explict = between the argument name and its value. For example:

  • Valid:
    • --foo=bar --cat-dog=fish
  • Invalid:
    • --foo bar --cat-dog fish

Implicit Booleans

If an argument value is omitted, it will be set as null. You can use this to accept implicit booleans (boolean arguments whose presence implies true) by using a z.union(). For example:

import { z } from "zod";
import { argumentParser } from "zodcli";

const options = z
	.object({
		foo: z
			.union([
				z.literal("true").transform(() => true),
				z.literal("false").transform(() => false),
				z.null().transform(() => true),
			])
			.default("false"),
	})
	.strict();

const result = argumentParser({
	options,
}).parse(["--foo"]);

console.log(result);
// { foo: true }

Optional Options

You can make options optional by using the .optional() modifier. For example:

import { z } from "zod";
import { argumentParser } from "zodcli";

const options = z
	.object({
		foo: z.string(),
		catDog: z.string().optional(),
	})
	.strict();

const result = argumentParser({
	options,
}).parse(["--foo=bar"]);

console.log(result);
// { foo: 'bar' }

Or, to make all options optional, you can use .partial() on the object. For example:

import { z } from "zod";
import { argumentParser } from "zodcli";

const options = z
	.object({
		foo: z.string(),
		catDog: z.string(),
	})
	.partial()
	.strict();

const result = argumentParser({
	options,
}).parse(["--foo=bar"]);

console.log(result);
// { foo: 'bar' }

Note, however, that for booleans, you will likely want to keep them required and simply provide a .default() false value. For example:

import { z } from "zod";
import { argumentParser } from "zodcli";

const options = z
	.object({
		foo: z
			.union([
				z.literal("true").transform(() => true),
				z.literal("false").transform(() => false),
				z.null().transform(() => true),
			])
			.default("false"),
		catDog: z.string(),
	})
	.partial()
	.required({ foo: true })
	.strict();

const result = argumentParser({
	options,
}).parse([]);

console.log(result);
// { foo: false }

Coercion

You can coerce non-string values using Zod. For example:

import { z } from "zod";
import { argumentParser } from "zodcli";

const options = z
	.object({
		foo: z.coerce.number(),
	})
	.strict();

const result = argumentParser({
	options,
}).parse(["--foo=2.2"]);

console.log(result);
// { foo: 2.2 }

Roadmap

  • [ ] Help message
  • [ ] Commands support
  • [ ] Positionals support
  • [ ] Object support
  • [x] Strict typing of aliases
  • [ ] Improve optionality/booleans