Giter VIP home page Giter VIP logo

cmdctr's Introduction

Command Center

github latest release npm version npm weekly downloads dependencies license open issues minzipped size follow on xitter

Command Center (cmdctr) is a tiny no-frills library for TypeScript and JavaScript that provides a simple, yet flexible way to create command line interfaces (CLI). It allows you to define commands with specific options and actions, and then run these commands from the command line. Type safety is built in, so you can be sure that your commands are being run with the correct options.

Warning

This project is still in early development and is not ready for production use.

What it is

cmdctr is a focused, streamlined tool for creating CLI apps without unnecessary complexity. It's heavily focused on inferred types and safety, making it ideal for rapid development. cmdctr is great for creating commands with specific options and actions, and executing those commands from the command line.

cmdctr has zero external dependencies, making it lightweight and easy to include in any project.

What it isn't

You won't find some of the cool features that often come bundled with CLI builder libraries. There are no color utilities, loading spinners, menus, events, or progress bars. Many established libraries for those needs already exist. Instead, cmdctr is built to work seamlessly alongside these specialized packages, allowing you to integrate only what you need.

Installation

bun i cmdctr
# or
npm i cmdctr

Or clone the repository:

git clone https://github.com/trvswgnr/cmdctr.git

Usage

Command Center provides three main functions: CmdCtr, Data, and Command.

CmdCtr

CmdCtr creates a new command center. It takes a string argument which is the name of the base command. This is the command that will be used to run commands.

import { CmdCtr } from "cmdctr";
const cmdCtr = CmdCtr("example");

Data

Data creates a new command data object. This object defines the name, description, and options for a command.

const command1Data = Data({
    name: "command-1",
    description: "A command that does something",
    options: {
        input: {
            short: "i",
            type: "string",
            description: "The input file to be processed",
            required: true,
        },
        output: {
            short: "o",
            type: "string",
            description: "The output file to be written",
            required: true,
        },
    },
});

Command

Command creates a new command. It takes a data object and an action function as arguments. The action function is what will be executed when the command is run.

const command1 = Command(command1Data, (opts) => {
    const { input, output } = opts;
    console.log(`input: ${input}`);
    console.log(`output: ${output}`);
});

A nice feature here is the options passed to the action function (opts here) are validated from the CLI and their types are known at compile-time. This means you get meaningfull type hints and code completion in your editor and can be sure that the arguments are the types you're expecting.

Registering and Running Commands

After creating commands, you can register them to the command center using the register method. Then, you can run the commands using the run method.

cmdCtr.register(command1);
cmdCtr.register(command2);
cmdCtr.run();

Setting the default command

You can set a default command to be run when no command is specified. This is done using the setDefault method.

cmdCtr.setDefault(command1);
// or
cmdCtr.setDefault("command-1");

Example

Here is a complete example of how to use Command Center:

// @ts-check
import { CmdCtr, Data, Command } from "cmdctr";
import ora from "ora"; // loading spinner (for funzies)

const cmdCtr = CmdCtr("example"); // or new CmdCtr(), if that's your thing

const command1Data = Data({
    name: "command-1",
    description: "A command that does something",
    options: {
        input: {
            short: "i",
            type: "string",
            description: "The input file to be processed",
            required: true,
        },
        output: {
            short: "o",
            type: "string",
            description: "The output file to be written",
            required: true,
        },
    },
});

const command1 = Command(command1Data, (opts) => {
    const { input, output } = opts;
    console.log(`input: ${input}`);
    console.log(`output: ${output}`);
});

const command2Data = Data({
    name: "command-2",
    description: "A command that does something else",
    options: {
        message: {
            short: "m",
            type: "string",
            description: "The message to be printed",
            required: true,
        },
        loud: {
            short: "l",
            type: "boolean",
            description: "Whether the message should be printed loudly",
            default: false,
        },
    },
});

const command2 = Command(command2Data, async (opts) => {
    const { message, loud } = opts;
    const loadingMsg = "...what was i saying again?";
    const spinner = ora(loadingMsg).start();
    const text = await new Promise((resolve) => {
        setTimeout(() => resolve(`oh yeah, ${loud ? message.toUpperCase() : message}`), 2000);
    });
    spinner.stop();
    console.log(text);
});

cmdCtr.register(command1);
cmdCtr.register(command2);
cmdCtr.setDefault(command2);
cmdCtr.run();

In this example, two commands are created: command-1 and command-2. command-1 takes an input file and an output file as options, and command-2 takes a message and a boolean flag as options. The commands are then registered to the command center and run.

In this case, command2 is registered AND set as the default command, so it will be run when no command is specified but can also be run explicitly by specifying command-2 as the command to run. If it had not been registered, it would run when no command is specified but would not be able to be run explicitly as a subcommand.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

cmdctr's People

Contributors

trvswgnr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jonnytest1

cmdctr's Issues

there should be documentation on how commands can be invoked :)

i found node <example>.js <args> in the example folder but i think there should a more detailed and more prevalent (in the main readme) version

something along the lines of

if this is your script:
...
then this is how you call it
...

with a few examples

maybe also a run.sh in the examples folder with a command for all scenarios that are in the example

create tests

this project desperately needs tests! hard to verify anything isn't breaking without them.

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.