Coder Social home page Coder Social logo

ask's Introduction

ask

Interactive command-line prompts for Deno.

Table of Contents

Description

ask is a slick and dependency-free Deno module that allows you to create interactive command-line applications, similar to what you'd achieve with inquirer in Node.js. Right now this module is very early in development, so please don't expect it to support everything inquirer does.

Overview

  • Supported prompts: input (text), confirm (yes/no response), number... and more to come!
  • Elegant output
  • Familiar, inquirer-like syntax
  • Easily configurable
  • Dependency-free

Basic Usage

It's very easy to get started. Just create an Ask instance and use the prompt() method to enumerate your questions.

import Ask from 'https://deno.land/x/ask/mod.ts';

const ask = new Ask(); // global options are also supported! (see below)

const answers = await ask.prompt([
  {
    name: 'name',
    type: 'input',
    message: 'Name:'
  },
  {
    name: 'age',
    type: 'number',
    message: 'Age:'
  }
]);

console.log(answers); // { name: "Joe", age: 19 }

You can also just ask a single question:

const { name } = await ask.input({
  name: 'name',
  message: 'Name:'
});

console.log(name); // Joe

Options

ask has options that you can pass to individual prompts.

General Options

These options are available for all question types.

  • name (string, required) - the name of the key in the returned object file that will contain the response to the question
  • type (string) - one of the supported types, defaults to "input"
  • message (string) - the message that will be displayed in the prompt. If not provided, the value of the name option will be displayed.
  • prefix (string) - the prefix that will be displayed before the question. Defaults to a green question mark.
  • suffix (string) - the suffix that will be displayed after the question. If no message is provided, it defaults to a colon :. Otherwise it defaults to an empty string.
  • input (Deno.Reader & Deno.ReaderSync & Deno.Closer) - the input buffer to accept answers. Defaults to Deno.stdin.
  • output (Deno.Writer & Deno.WriterSync & Deno.Closer) - the output buffer used to display the questions. Defaults to Deno.stdout.
  • validate (val?: any) => Promise | boolean - a function that can be used to validate the user input. Defaults to a function that always returns true.

Global Options

These options will apply to every prompt in a given Ask instance, unless overwritten:

  • prefix (string)
  • suffix (string)
  • input (Deno.Reader & Deno.ReaderSync & Deno.Closer)
  • output (Deno.Writer & Deno.WriterSync & Deno.Closer)

Example:

import Ask from 'https://deno.land/x/ask';

const ask = new Ask({
  prefix: '>'
});

const answers = await ask.prompt([
  {
    name: 'name',
    message: 'Your name:',
    type: 'input'
  },
  {
    name: 'age',
    message: 'Your age:',
    type: 'number',
    prefix: '?'
  }
]);

// > Your name:
// ? Your age:

Confirm

  • accept (string) - the character/sequence used as the "yes" answer. This will also be the default value. The function will only return true if the user entered this string or nothing at all. Defaults to "Y".
  • deny (string) - the character/sequence used as the "no" answer. Doesn't really matter what you put here, it's mainly there for stylistic reasons. Defaults to "n".

Number

  • min (number) - the minimum value that the user can enter. Defaults to -Infinity.
  • max (number) - the maximum value that the user can enter. Defaults to Infinity.

Depending on which of these parameters are provided, the function will print different output:

// if none are provided:
'message'

// if `min` is provided, but `max` is not:
'message (>= min)'

// if `max` is provided, but `min` is not:
'message (<= max)'

// if both are provided:
'message [min-max]'

Right now, this behavior is not configurable, but if there's popular demand for it, I could add it as a configuration option.

TODOs

  • Global configuration
  • hidden type
  • masked type
  • list type
  • Unit tests

License

MIT.

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.