Git Product home page Git Product logo

deno-prompt's Introduction

deno-prompt

A command line interaction utils like inquirer.

params

filed type required description default
type string Y Type that will use different components.
name string Y Field name in response object.
message string N Toast message before user input. First letter upper of name.
defaultValue any N Default value. undefined
validate Function N Validate user input. Throw PromptError if deny. Function that returns void.
when Function N Returns true or Promise to show this prompt. Function that returns true.

builtin types

  • text Read user input.
  • number Read user input which must be a number. Extra params like this:
filed type required description default
min number N Min number.
max number N Max number.
  • confirm Read user input and returns as boolean. Extra params like this:
filed type required description default
yes string N Yes string.
no string N No string.

basic usages

import Prompt from "https://deno.land/x/prompt";

const answers = await Prompt.prompts([
  { type: "text", name: "name", message: "Please input your name: " },
  {
    type: "text",
    name: "sex",
    message: "Please input your sex(male or female): ",
    validate(result: string) {
      if (!["male", "female"].includes(result)) {
        throw new PromptError("input must be [male] or [female]");
      }
    },
  },
  { type: "number", name: "birthYear", min: 1900 },
  { type: "number", name: "age", min: 1, max: 100 },
  { type: "confirm", name: "agree", defaultValue: true },
]);

console.log(answers);

custom usages

It supported to define custom component. Here is am example.

  • Define component which must has static function called run and accept args which extends PromptParams.
import Prompt from "https://deno.land/x/prompt";

class CustomComponent {
  static async run<T extends PromptParams>(args: T) {
    // Your define code to do something.
  }
}

Prompt.registerComponent("custom", CustomComponent);
  • Usage.(You must put bellow code after define component)
import Prompt from "https://deno.land/x/prompt";

const answers = await Prompt.prompts([
  { type: "custom", name: "name", message: "Please input your name: " },
]);

console.log(answers);

deno-prompt's People

Contributors

akunchen avatar

Watchers

 avatar  avatar

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.