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 🙏

© 2024 – Pkg Stats / Ryan Hefner

climonad

v0.3.0

Published

A high-performance, low-overhead library for building modern command-line interfaces in Node.js

Downloads

194

Readme

Climonad.js

[!WARNING] This library is in early development, and APIs may change without notice.

A high-performance, low-overhead library for building modern command-line interfaces in Node.js.

Features

  • High-Performance: Lightning-fast CLI argument parsing
  • Flexible: Easy-to-define commands and options
  • Extensible: Lightweight, modular architecture with a simple API
  • Powerful: Support for aliases, flags, and custom options
  • Required Flags: Enforce mandatory options for commands

Quick Start

Define Your CLI

import { Cli } from "climonad"

const cli = Cli.createCli({
  name: "my-app",
  description: "A powerful CLI application",
  commands: [
    Cli.cmd({ name: "init", description: "Initialize the project" }),
    Cli.cmd({
      name: "build",
      description: "Build the project",
      options: [
        Cli.str({
          name: "output",
          flag: "--out",
          alias: "-o",
          description: "Set output directory",
        }),
      ],
    }),
  ],
  options: [
    Cli.bool({
      name: "verboseOption",
      flag: "--verbose",
      description: "Enable verbose output",
    }),
  ],
})

cli.run(process.argv.slice(2))

Argument Parsing and Handling

Climonad uses declarative configuration to define and parse CLI arguments:

Commands

Commands represent distinct functionalities, like "build" or "serve." Define them using Cli.cmd() and assign descriptions, aliases, and options. For example:

Cli.cmd({ name: "serve", description: "Start the development server", alias: "s" })

Invoke commands using their name or alias:

my-app serve
my-app s

Options

Options modify command behavior and can be defined as:

  • Boolean Flags: Toggle features on or off.
  • String Options: Accept string values.
  • Number Options: Accept numeric inputs.
  • Required Flags: Mark options as required, enforcing their presence.
  • Default Values: Provide fallback values when options are not specified.

Example:

Cli.str({
  name: "host",
  flag: "--host",
  description: "Specify the hostname",
  default: "localhost",
})
Cli.num({
  name: "port",
  flag: "--port",
  description: "Set the port number",
})
Cli.bool({
  name: "verbose",
  flag: "--verbose",
  description: "Enable verbose logging",
})
Cli.str({
  name: "config",
  flag: "--config",
  description: "Configuration file",
  required: true, // Marking the option as required
})

Pass options as:

my-app serve --host localhost --port 8080 --verbose
my-app serve --config app.config

If the required flag is missing, climonad will throw an error.

Parsing Logic

  • Positional Arguments: Commands are identified by position (e.g., my-app serve).
  • Flag Arguments: Options prefixed with -- or aliases like -v are parsed.
  • Default Values: If a flag is not provided, Climonad uses the default value.

Example:

const result = cli.parse(["serve", "--host", "example.com", "--port", "3000"])

Produces:

{
  commands: Set(1) { "serve" },
  options: Map(2) {
    "host": "example.com",
    "port": 3000,
  },
  generateHelp: [Function],
}

Auto Help Generation

Invoke -h or --help to display detailed help:

  • Global Help: Provides help for the entire CLI.
  • Command-Scoped Help: Displays help specific to a command.

Example:

my-app --help
my-app serve --help

Error Handling

Climonad includes robust error handling:

  • Invalid commands or options throws a CliError.
  • Missing required flags will result in an error.
  • Invalid values for typed options (e.g., --port not-a-number) raise descriptive errors.

Performance

[!NOTE] Metrics are preliminary and subject to change as the library evolves.

Benchmarks conducted using Deno's bench:

| Operation | Time (avg) | Ops/Second | | ----------------------- | -------------- | -------------- | | CLI Initialization | ~725.4 ns | 1,379,000 | | Basic Command Execution | ~190.5 ns | 5,249,000 | | Command with Options | ~654.5 ns | 1,528,000 |

Algorithmic Complexity

  • CLI Initialization: O(n) for commands and options.
  • Command/Option Lookup: O(1) with optimized caching.
  • Argument Parsing: O(n) for input arguments.
  • Help Generation: O(m) for scoped commands/options.
  • Space Complexity: O(n) with minimal overhead.

Efficient for small scripts and large CLI applications alike.

Contributing

We welcome contributions! Here's how you can help:

  1. Report Bugs: Found a bug? Open an issue.
  2. Suggest Features: Got an idea? Let us know.
  3. Submit Pull Requests:
    • Fork the repository.
    • Create a feature branch.
    • Submit a pull request.

License

Licensed under the MIT License.