ask
Interactive command-line prompts for Deno.
Table of Contents
Description
ask
is a slick 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
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@1.0.6/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 questiontype
(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 thename
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 nomessage
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 toDeno.stdin
.output
(Deno.Writer & Deno.WriterSync & Deno.Closer) - the output buffer used to display the questions. Defaults toDeno.stdout
.validate
(val?: any) => Promise | boolean - a function that can be used to validate the user input. Defaults to a function that always returnstrue
.
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 toInfinity
.
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.