User input
For complete API documentation refer to: pub.dev
Asking the user to input data into a CLI application should be simple. DCli provide a number of core methods to facilitate user input.
ask
confirm
menu
Ask
The 'ask' function provides a simple but flexible means of requesting information from the user.
In its simplest form, you can ask the user for an input string.
Any user input whitespace is stripped before it is validated or returned.
Arguments
prompt
The prompt is the only positional argument that ask
takes. Ask will display the prompt verbatim.
You may pass an empty string for the prompt in which case no prompt will be displayed.
hidden
You can request that the user input isn't echoed back to the user:
defaultValue
You can provide a default value. If the user hits enter without entering any text then the default value will be returned.
If you combine a defaultValue with the hidden argument then the default value will be rendered as 6 '*'.
If you combine a defaultValue with an empty prompt then Ask will not display the prompt nor the default value.
See the 'customPrompt' argument to modify how the default is displayed.
validator
Ask takes a validator. If the entered input doesn't match the supplied validator then the user will be re-prompted until they enter a valid value.
See the section on validators for more details.
required
By default, the ask function requires the user to enter a non-blank line (whitespace is stripped from user input before it is evaluated).
If you want to make a user value optional either pass in a defaultValue or pass required: false
customPrompt
Since: 2.0.0
By default when passing a default value the ask
command formats the default within brackets:
You can completely modify the prompt by providing the customPrompt
argument.
Be careful to suppress displaying the default value when hidden
is true, otherwise, you may end up displaying a password.
Confirm
The confirm method allows you to ask the user for a true/false response and returns a bool reflecting what the user entered.
Arguments
prompt
The prompt is the only positional argument that confirm
takes. Confirm will display the prompt verbatim.
You may pass an empty string for the prompt in which case no prompt will be displayed.
defaultValue
You can provide a default value. If the user hits enter without entering any text then the default value will be returned.
The default value is capitalized.
customPrompt
Since: 2.0.0
By default when passing a default value the confirm
command formats the default within brackets:
You can completely modify the prompt by providing the customPrompt
argument.
Are you sure?> [yes/No]
Menu
The menu function allows you to display a list of menu items for the user to select from and returns the selected item.
You can also specify a default option. If you pass a default value and the user hits enter without entering a value then the default value will be returned.
The list of options can be a String or a Dart class. By default menu will call toString on any object passed but you can pass the format argument to control how each option is displayed:
Arguments
options
A list of options for the user to select from.
The list can be a list of Strings or a list of Dart objects (all of the same type).
defaultOption
Specifies the defaultOption from the list of options
. The defaultOption
must be of the same type as the items in the options
list.
The default option will be colored-coded in the list if your terminal supports ANSI escape codes.
The default option will be displayed as an index after the prompt.
format
By default the menu function will display each option by calling toString
on the passed option.
You can provide an alternate formatter for each option by passing a lambda to the format argument.
customPrompt
Since: 2.0.0
The customPrompt allows you to modify the selection prompt.
limit
If you pass in a large option
list you can pass in the limit
argument to limit the number of options displayed in the menu. The first limit
options in the list of options will be displayed.
fromStart
FromStart is true by default. If you set it to false and you pass a limit
then the menu will show the last limit
options.
Last updated