reloquent
Reloquent allows to ask users a question, a confirmation (y/n), or a series of questions via the read-line interface.
yarn add reloquentnpm i reloquent
Table Of Contents
- Table Of Contents
- API
Question
Typeasync askSingle(question: (string|!Question), timeout=: number): string
async askQuestions(questions: !Questions, timeout=: number): !Object<string, string>
async confirm(question: (string|!Question), options=: !ConfirmOptions): boolean
- Copyright
API
There are 4 types of calls to the API:
- ask a single question as a string;
- ask a single question as an object;
- ask multiple questions.
- ask for a confirmation;
Their respective methods can be accessed via the import
statement:
Question
Type
When asking a question which is not a string, the question
object should have the following structure:
Property | Type | Description | Example |
---|---|---|---|
text* |
string | Display text. Required. |
|
validation |
(async) function | A function which needs to throw an error if validation does not pass. |
|
postProcess |
(async) function | A function to transform the answer. |
|
defaultValue |
string |
Default answer (shown to users in |
|
getDefault |
(async) function | A function to execute to obtain the default value. |
|
password |
boolean | Hide the inputs behind * when typing the answer. |
|
If both defaultValue
and getDefault
are provided, the result of the getDefault
takes precedence:
const q = defaultValue: 'I desire it much' { return 'I desire it much so' }
When the password
property is set to true, the answer will be hidden behind the *
symbols.
const Password = async { const res = await return res}
Please enter the password: ********
Question
extends readline.ReadLineOptions
: A question.
Name | Type | Description | Default |
---|---|---|---|
text* | string | The text to show to the user. | - |
defaultValue | string | The default answer to the question. | - |
password | boolean | Hide the inputs behind * when typing the answer. |
false |
getDefault | () => (string | !Promise<string>) | The function which will get the default value, possibly asynchronously. | - |
validation | (answer: string) => void | The validation function which should throw on error. | - |
postProcess | (answer: string) => (string | !Promise<string>) | The transformation function for the answer. | - |
async askSingle(
question: (string|!Question),
timeout=: number,
): string
Ask user a question via the CLI. Returns the answer to the question. If a timeout is passed, the promise will expire after the specified number of milliseconds if the answer was not given.
- question*
(string | !Question)
: The question to present to the user. - timeout
number
(optional): How long to wait before rejecting the promise. Waits forever by default.
Questions can be asked as a simple string.
async { try const answer = await console catch err console console console }
What brought you her: I guess Art is the cause.
You've answered: I guess Art is the cause.
Alternatively, Reloquent can ask a question which is passed as an object of the Question
type, and return a string.
async { const answer = await console}
Do you wish me to stay so long? [I desire it much]
I desire it much!
async askQuestions(
questions: !Questions,
timeout=: number,
): !Object<string, string>
Ask user a series of questions via CLI and transform them into answers. Returns an object with keys as questions' texts and values as answers.
- questions*
!Questions
: A set of questions. - timeout
number
(optional): How long to wait before rejecting the promise. Waits forever by default.
!Object<string, (string | !Question)>
Questions
: A set of questions.
const Ask = async { const questions = title: text: 'Title' { if !a throw 'Please enter the title.' } description: text: 'Description' s defaultValue: 'A test default value' date: text: 'Date' async { await return } const res = await return res}
If when provided with the following answers (leaving Date as it is), the result will be returned as an object:
Title: hello
Description: [A test default value] world
Date: [2/22/2020, 21:37:04]
Result: {
"title": "hello",
"description": "world",
"date": "2/22/2020, 21:37:04"
}
async confirm(
question: (string|!Question),
options=: !ConfirmOptions,
): boolean
Ask a yes/no question. Returns true
when answer was y
and false
otherwise.
- question*
(string | !Question)
: The question, such as "Add default options", or "Continue to delete?". The question mark can added automatically. - options
!ConfirmOptions
(optional): Options for the confirmation question.
ConfirmOptions
: Options for the confirmation question.
Name | Type | Description | Default |
---|---|---|---|
defaultYes | boolean | Whether the default value is yes. | true |
timeout | number | How long to wait before rejecting the promise. Waits forever by default. | - |
const Confirm = async { const res = await return res}
Do you wish to continue (y/n): [n] y
Result: true
Copyright
© Art Deco™ 2020 |
---|