Code Monkey home page Code Monkey logo

zcli's Introduction

zCLI

A framework for building type-safe command-line tools powered by Zod

Features

  • Zod validations for POSIX-compliant flags and arguments
  • Declarative API
  • S-tier type-safety and autocomplete
  • Built-in help command and --help flag
  • Built-in version command
  • Built-in completion command for Bash, Fish, and Zsh
  • Type-safe, persistent configuration
  • Type-safe, persistent key-value cache
  • Type-safe environment variables
  • Built-in internationalization using the user's locale and Deno's Intl API
  • Global flags
  • Automated README generation
  • Command and flag aliases
  • persistentPreRun, preRun, postRun, persistentPostRun hooks

Getting started

The easiest way to get started is to use the zCLI CLI to generate a new project.

# Install the zCLI CLI
curl -fsSL https://raw.githubusercontent.com/jaredLunde/zcli-cli/main/install.sh | sh

# Create a new project
zcli init my-project

# Add a command
zcli add my-command

Example usage

import { args, env, flag, flags, init } from "https://deno.land/x/zcli/mod.ts";
import { z } from "https://deno.land/x/zcli/z.ts";

const cli = init({
  globalFlags: flags({
    verbose: flag({ aliases: ["v"] }).oboolean(),
    raw: flag({ aliases: ["r"] }).oboolean(),
  }),

  ctx: {
    env: env({
      DEBUG: env.bool().default("false"),
    }),

    meta: {
      version: "1.3.2",
      build: Deno.build,
      commit: "development",
      date: new Date().toISOString(),
    },
  },
});

const fetcher = cli
  .command("fetcher", {
    args: args({
      short: "The URL to fetch",
    }).tuple([z.string().url()]),

    flags: flags({
      method: flag({ short: "The HTTP method to use", aliases: ["m"] })
        .enum(["POST", "GET", "PUT", "PATCH", "DELETE", "HEAD"])
        .default("GET"),
      headers: flag({ short: "Add headers to the request", aliases: ["H"] })
        .array(z.string())
        .optional(),
      data: flag({
        short: "Send request data",
        aliases: ["d"],
      }).ostring(),
    }),
  })
  .describe("Fetch a resource from the internet")
  .preRun(({ flags, ctx }) => {
    if (ctx.env.get("DEBUG")) {
      console.log("Fetching:", flags.url);
    }
  })
  .run(async ({ flags, ctx }) => {
    if (ctx.env.get("DEBUG")) {
      console.log("Fetching:", flags.url);
    }

    const response = await fetch(flags.url, {
      method: flags.method,
      headers: new Headers(
        flags.headers?.map((h) => h.split(":").map((s) => s.trim())),
      ),
      body: flags.data,
    });

    if (flags.json) {
      console.log(await response.json());
    } else {
      console.log("Response:", response);
    }
  });

if (import.meta.main) {
  await fetcher.execute();
}

The problem

Command-line tools written in Node and Deno suffer from a few major issues:

  • They are difficult to test
  • They are difficult to type
  • Their boot time is slow
  • Distribution is difficult

The solution

This framework aims to solve these issues by providing a declarative, simple API for building type-safe commandline tools using Zod and Deno. It is largely inspired by what tRPC did for RPC APIs, hence the nod to the name.

Deno was chosen as the runtime because it is fast, secure, and has a great standard library. It also has a great testing story and is easy to distribute, as you can use deno compile to create a single binary. In the future, we may also support compiling with bun to create a single binary.

Inherent boot performance is achieved by limiting the amount of code that is bundled into the binary. The flags parser is also about 7x faster than the Deno standard library's parser, though this amounts to a small fraction of the total boot/execution time.

benchmark          time (avg)             (min … max)       p75       p99      p995
----------------------------------------------------- -----------------------------
zcli.parse()     1.08 µs/iter   (986.08 ns … 1.37 µs)   1.11 µs   1.37 µs   1.37 µs
flags.parse()    7.69 µs/iter     (5.22 µs … 6.15 ms)   6.17 µs  19.37 µs  48.85 µs

summary
  zcli.parse()
   7.11x faster than flags.parse()

zcli's People

Contributors

jaredlunde avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

zcli's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.