# new UserInput(name)
This class provides a flexible interface for creating and managing user input prompts. It implements the PromptObject interface from the 'prompts' library and offers methods to set various properties of the prompt. The class also includes static methods for common input scenarios and argument parsing.
Represents a user input prompt with various configuration options.
Parameters:
Name | Type | Description |
---|---|---|
name |
The name of the prompt, used as the key in the returned answers object. |
Example
```typescript
import { UserInput } from '@decaf-ts/utils';
// Create a simple text input
const nameInput = new UserInput('name')
.setMessage('What is your name?')
.setInitial('User');
// Create a number input with validation
const ageInput = new UserInput('age')
.setType('number')
.setMessage('How old are you?')
.setMin(0)
.setMax(120);
// Ask for input and process the results
async function getUserInfo() {
const answers = await UserInput.ask([nameInput, ageInput]);
console.log(`Hello ${answers.name}, you are ${answers.age} years old.`);
}
getUserInfo();
```
Classes
- UserInput
This class provides a flexible interface for creating and managing user input prompts. It implements the PromptObject interface from the 'prompts' library and offers methods to set various properties of the prompt. The class also includes static methods for common input scenarios and argument parsing.
Members
# active
The style applied to the currently selected option.
The active option style for select inputs.
string
|
PrevCaller.<R, (string|Falsy)>
|
undefined
# active
The style applied to the currently selected option.
The active option style for select inputs.
# choices
An array of options that the user can select from in choice-based prompts.
The available choices for select, multiselect, or autocomplete inputs.
Array
|
PrevCaller.<R, (Array|Falsy)>
|
undefined
# choices
An array of options that the user can select from in choice-based prompts.
The available choices for select, multiselect, or autocomplete inputs.
boolean
|
PrevCaller.<R, (boolean|Falsy)>
|
undefined
# float
If true, allows decimal numbers.
Whether to allow float values for number inputs.
# format
A function to format the user's input before it's returned.
The format function for the input.
PrevCaller.<R, void>
|
undefined
# format
A function to format the user's input before it's returned.
The format function for the input.
string
|
PrevCaller.<R, (string|Falsy)>
|
undefined
# hint
Additional information displayed to the user.
The hint text for the prompt.
string
|
PrevCaller.<R, (string|Falsy)>
|
undefined
# inactive
The style applied to non-selected options.
The inactive option style for select inputs.
# increment
The step size when increasing or decreasing the number.
The increment value for number inputs.
number
|
PrevCaller.<R, (number|Falsy)>
|
undefined
# increment
The step size when increasing or decreasing the number.
The increment value for number inputs.
InitialReturnValue
|
PrevCaller.<R, (InitialReturnValue|Promise.<InitialReturnValue>)>
|
undefined
# initial
The default value presented to the user.
The initial value of the prompt.
string
|
boolean
|
undefined
# instructions
Additional guidance provided to the user.
Instructions for the user.
number
|
PrevCaller.<R, (number|Falsy)>
|
undefined
# limit
The maximum number of items that can be selected.
The limit for list inputs.
string
|
PrevCaller.<R, (string|Falsy)>
|
undefined
# mask
The character used to hide the user's input.
The mask for password inputs.
number
|
PrevCaller.<R, (number|Falsy)>
|
undefined
# max
The highest number the user can input.
The maximum value for number inputs.
ValueOrFunc.<string>
|
undefined
# message
The question or instruction presented to the user.
The message displayed to the user.
number
|
PrevCaller.<R, (number|Falsy)>
|
undefined
# min
The lowest number the user can input.
The minimum value for number inputs.
PrevCaller.<R, void>
|
undefined
# onState
A function called when the state of the prompt changes.
The onState callback function.
# round
Determines the precision of float inputs.
The number of decimal places to round to for float inputs.
number
|
PrevCaller.<R, (number|Falsy)>
|
undefined
# round
Determines the precision of float inputs.
The number of decimal places to round to for float inputs.
string
|
PrevCaller.<R, (string|Falsy)>
|
undefined
# separator
The character used to separate list items.
The separator for list inputs.
Writable
|
undefined
# stdout
The output stream used by the prompt.
The stdout stream for the prompt.
string
|
PrevCaller.<R, (string|Falsy)>
|
undefined
# style
Determines the visual style of the prompt.
The style of the prompt.
PromptType
|
Falsy
|
PrevCaller.<R, (PromptType|Falsy)>
# type
Determines the input method (e.g., text, number, confirm).
The type of the prompt.
PrevCaller.<R, (boolean|string|Promise.<(boolean|string)>)>
|
undefined
# validate
A function to validate the user's input.
The validation function for the input.
string
|
PrevCaller.<R, (string|Falsy)>
|
undefined
# warn
A warning message displayed to the user.
The warning text for the prompt.
Methods
# async ask()
Prompts the user and returns their response as a single value.
Asks the user for input based on the current UserInput configuration.
A Promise that resolves to the user's answer.
# setActive(value)
Configures the style applied to the currently selected option.
Sets the active option style for select inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The active option style. |
This UserInput instance for method chaining.
# setChoices(value)
Configures the available options that the user can select from in choice-based prompts.
Sets the choices for select, multiselect, or autocomplete inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The array of choices or a function to determine the choices. |
This UserInput instance for method chaining.
# setFloat(value)
Configures whether decimal numbers are allowed.
Sets whether to allow float values for number inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
Whether to allow float values. |
This UserInput instance for method chaining.
# setFormat(value)
Configures a function to format the user's input before it's returned.
Sets the format function of the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The format function. |
This UserInput instance for method chaining.
# setHint(value)
Configures additional information displayed to the user.
Sets the hint text for the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The hint text. |
This UserInput instance for method chaining.
# setInactive(value)
Configures the style applied to non-selected options.
Sets the inactive option style for select inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The inactive option style. |
This UserInput instance for method chaining.
# setIncrement(value)
Configures the step size when increasing or decreasing the number.
Sets the increment value for number inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The increment value. |
This UserInput instance for method chaining.
# setInitial(value)
Configures the default value presented to the user.
Sets the initial value of the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The initial value. |
This UserInput instance for method chaining.
# setInstructions(value)
Configures additional guidance provided to the user.
Sets the instructions for the user.
Parameters:
Name | Type | Description |
---|---|---|
value |
The instructions. |
This UserInput instance for method chaining.
# setLimit(value)
Configures the maximum number of items that can be selected in list-type prompts.
Sets the limit for list inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The maximum number of items that can be selected, or a function to determine this value. |
This UserInput instance for method chaining.
# setMask(value)
Configures the character used to hide the user's input in password-type prompts.
Sets the mask for password inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The character used to mask the input, or a function to determine this value. |
This UserInput instance for method chaining.
# setMax(value)
Configures the highest number the user can input.
Sets the maximum value for number inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The maximum value. |
This UserInput instance for method chaining.
# setMessage(value)
Configures the question or instruction presented to the user.
Sets the message of the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The message to be displayed. |
This UserInput instance for method chaining.
# setMin(value)
Configures the lowest number the user can input.
Sets the minimum value for number inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The minimum value. |
This UserInput instance for method chaining.
# setOnState(value)
Configures a function to be called when the state of the prompt changes.
Sets the onState callback of the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The onState callback function. |
This UserInput instance for method chaining.
# setRound(value)
Configures the precision of float inputs.
Sets the number of decimal places to round to for float inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The number of decimal places. |
This UserInput instance for method chaining.
# setSeparator(value)
Configures the character used to separate list items.
Sets the separator for list inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
The separator character. |
This UserInput instance for method chaining.
# setStdin(value)
Configures the input stream used by the prompt for receiving user input.
Sets the stdin stream for the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The Readable stream to be used as stdin. |
This UserInput instance for method chaining.
# setStdout(value)
Configures the output stream used by the prompt for displaying messages and results.
Sets the stdout stream for the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The Writable stream to be used as stdout. |
This UserInput instance for method chaining.
# setStyle(value)
Configures the visual style of the prompt.
Sets the style of the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The style to be applied. |
This UserInput instance for method chaining.
# setSuggest(value)
Configures a function that provides suggestions based on the user's input and available choices.
Sets the suggestion function for autocomplete inputs.
Parameters:
Name | Type | Description |
---|---|---|
value |
A function that takes the current input and available choices and returns a Promise resolving to suggestions. |
This UserInput instance for method chaining.
# setType(type)
Configures the input method for the prompt.
Sets the type of the prompt.
Parameters:
Name | Type | Description |
---|---|---|
type |
The type of the prompt. |
This UserInput instance for method chaining.
# setValidate(value)
Configures a function to validate the user's input.
Sets the validation function of the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The validation function. |
This UserInput instance for method chaining.
# setWarn(value)
Configures a warning message displayed to the user.
Sets the warning text for the prompt.
Parameters:
Name | Type | Description |
---|---|---|
value |
The warning text. |
This UserInput instance for method chaining.
# async static ask(question)
Prompts the user with one or more questions and returns their answers as an object.
Asks the user one or more questions based on the provided UserInput configurations.
Parameters:
Name | Type | Description |
---|---|---|
question |
A single UserInput instance or an array of UserInput instances. |
A Promise that resolves to an object containing the user's answers.
# async static askConfirmation(name, question, initial)
Prompts the user with a yes/no question and returns a boolean result.
Asks the user for a confirmation (yes/no).
Parameters:
Name | Type | Description |
---|---|---|
name |
The name of the prompt, used as the key in the returned answers object. |
|
question |
The message displayed to the user. |
|
initial |
The initial value presented to the user (optional). |
A Promise that resolves to a boolean representing the user's answer.
# async static askNumber(name, question, min, max, initial)
Prompts the user to enter a number, with optional minimum, maximum, and initial values.
Asks the user for a number input.
Parameters:
Name | Type | Description |
---|---|---|
name |
The name of the prompt, used as the key in the returned answers object. |
|
question |
The message displayed to the user. |
|
min |
The minimum allowed value (optional). |
|
max |
The maximum allowed value (optional). |
|
initial |
The initial value presented to the user (optional). |
A Promise that resolves to the number entered by the user.
# async static askText(name, question, mask, initial)
Prompts the user to enter text, with optional masking and initial value.
Asks the user for a text input.
Parameters:
Name | Type | Description |
---|---|---|
name |
The name of the prompt, used as the key in the returned answers object. |
|
question |
The message displayed to the user. |
|
mask |
The character used to mask the input (optional, for password-like inputs). |
|
initial |
The initial value presented to the user (optional). |
A Promise that resolves to the text entered by the user.
# async static insist(input, test, defaultConfirmation, limit)
This method insists on getting a valid input from the user, allowing for a specified number of attempts.
Repeatedly asks for input until a valid response is given or the limit is reached.
Parameters:
Name | Type | Description |
---|---|---|
input |
The UserInput instance to use for prompting. |
|
test |
A function to validate the user's input. |
|
defaultConfirmation |
The default value for the confirmation prompt (true for yes, false for no). |
|
limit |
The maximum number of attempts allowed (default is 1). |
A Promise that resolves to the valid input or undefined if the limit is reached.
# async static insistForNumber(name, question, test, min, max, initial, defaultConfirmation, limit)
This method insists on getting a valid number input from the user, allowing for a specified number of attempts.
Repeatedly asks for number input until a valid response is given or the limit is reached.
Parameters:
Name | Type | Description |
---|---|---|
name |
The name of the prompt, used as the key in the returned answers object. |
|
question |
The message displayed to the user. |
|
test |
A function to validate the user's input. |
|
min |
The minimum allowed value (optional). |
|
max |
The maximum allowed value (optional). |
|
initial |
The initial value presented to the user (optional). |
|
defaultConfirmation |
The default value for the confirmation prompt (true for yes, false for no). |
|
limit |
The maximum number of attempts allowed (default is -1, meaning unlimited). |
A Promise that resolves to the valid input or undefined if the limit is reached.
# async static insistForText(name, question, test, mask, initial, defaultConfirmation, limit)
This method insists on getting a valid text input from the user, allowing for a specified number of attempts.
Repeatedly asks for text input until a valid response is given or the limit is reached.
Parameters:
Name | Type | Description |
---|---|---|
name |
The name of the prompt, used as the key in the returned answers object. |
|
question |
The message displayed to the user. |
|
test |
A function to validate the user's input. |
|
mask |
The character used to mask the input (optional, for password-like inputs). |
|
initial |
The initial value presented to the user (optional). |
|
defaultConfirmation |
The default value for the confirmation prompt (true for yes, false for no). |
|
limit |
The maximum number of attempts allowed (default is -1, meaning unlimited). |
A Promise that resolves to the valid input or undefined if the limit is reached.
# static parseArgs(options)
Uses Node.js's util.parseArgs to parse command-line arguments and return the result.
Parses command-line arguments based on the provided options.
Parameters:
Name | Type | Description |
---|---|---|
options |
Configuration options for parsing arguments. |
An object containing the parsed arguments.