Skip to main content

Built and signed on GitHub Actions

A framework for building command line applications with Deno.

This package works with Deno
This package works with Deno
JSR Score
100%
Published
2 months ago (0.3.1)

CLI

CLI is a small framework for building CLI applications built upon Deno 2's new standard library, inspired by similar libraries in the Golang ecosystem. When starting new Golang projects, I find myself installing log and cli, or writing a lot of boilerplate for standard library argument parsing and logging. Given Deno's similarities to the Golang ecosystem, I thought it would be fun to write my first library creating a simple, configurable interface to build your applications.

Installation

This package is hosted on the JSR. To install it in your project, run:

deno add jsr:@desertthunder/cli

Examples

Creating an Application

The fastest way to get started is to import the helper functions. These are simple wrappers around the constructor for the Command class. Make note of the Metadata type when defining the attributes and interface of your application.

import { createApplication, createCommand, Flags } from "jsr:@desertthunder/cli";

// Define flags for your command (optional)
const flags = [
  new Flag('string', 'name', 'name of something', true),
  new Flag('string', 'type', 'type of something', true),
];

// Define an action to call when your command is run.
function exampleAction(args?: Record<string, any>) {
  if (!args) {
    console.log("No args passed!");
    return;
  }

  console.log(JSON.stringify(args, null, 2));
},

// Define metadata for your command
const exampleCommand = createCommand(
  { name: 'command', usage: 'do something' }, action, flags
)

// Create a root command
const root = createApplication(
  { name: 'application', usage: '' }, [exampleCommand]
);

// if __name__ == '__main__' ?
if (import.meta.main) {
  root.run(Deno.args);
}

Logger

function example() {
  const logger = createLogger("example-logger");

  logger.info("Example");
}

if (import.meta.main) {
  example();
}

Colors

console.log(colorizeText("This is red text", TerminalTextColor.RED));
console.log(
  colorizeText('This is (bright) cyan text', TerminalTextColor.BRIGHT_CYAN),
);

// See src/libs/logger/colors.ts for the full code block of examples.
// Run deno run src/libs/logger/colors.ts the see the examples with your
// terminal emulator's color scheme

colors in stdout

Runtime Compatibility

Runtime Version Works Notes
Deno 2.1.4

Inspiration

  • Charm's log library
  • Urfave's cli V2 & V3

License

Copyright 2024 Owais J (mailto:desertthunder.dev@gmail.com). See LICENSE for more information.

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@desertthunder/cli

Import symbol

import * as cli from "@desertthunder/cli";

---- OR ----

Import directly with a jsr specifier

import * as cli from "jsr:@desertthunder/cli";