Giter Club home page Giter Club logo

autocli's Introduction

AutoCLI

A small library for creating CLIs in TypeScript

Usage example

import { CLI, CLIArgs } from "/path/to/AutoCLI/mod.ts";

const cli = new CLI([
  ["command1", "Run handler1", handler1],
  ["command2 foo", "Run handler2_1 with an argument named foo", handler2_1],
  [
    "command2 foo bar",
    "Run handler2_2 with 2 arguments, named foo and bar",
    handler2_2,
  ],
]);

function handler1({}: CLIArgs) {
  console.log("running handler1");
}

function handler2_1({ foo }: CLIArgs) {
  console.log(`running handler2_1 with foo=${foo}`);
}

function handler2_2({ foo, bar }: CLIArgs) {
  console.log(`running handler2_2 with foo=${foo} and bar=${bar}`);
}

cli.setPrompt(">>");
cli.run();

Run with sample inputs:

$ deno run example.ts
? >> help
command1: Run handler1
command2 foo: Run handler2_1 with an argument named foo
command2 foo bar: Run handler2_2 with 2 arguments, named foo and bar

? >> command1
running handler1
? >> command1 test
Invalid command 'command1 test'. Type 'help' for a list of commands.
? >> command2
Invalid command 'command2'. Type 'help' for a list of commands.
? >> command2 test
running handler2_1 with foo=test
? >> command2 test1 test2
running handler2_2 with foo=test1 and bar=test2
? >> command3
Unknown command 'command3'. Type 'help' for a list of commands.
? >> quit

The help list, invalid command message, and unknown command message are are internal. The invalid and unknown messages can be overwritten with cli.setInvalid((cmd: string)=>{...}); and cli.setUnknown((cmd: string)=>{...});.

TODO: Support optional arguments, e.g. ['command2 [foo [bar]]','Run handler2 with optional arguments, accepting first foo then bar', handler2]. Unassigned arguments would be set to undefined.

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.