The simple_prompter package defines an automatic, easy-to-use and stylish cmd/terminal wizard maker for Dart. It can create multiple choice questions without any limits along with yes/no questions.

Installation:

Be sure to follow the instructions mentioned on the Installing tab to easily install and import the package.

Usage

Asking a multiple-choice question:

  1. Create a new instance of Prompter() class
final prompter = new Prompter();
  1. Then create your options using the Option() class. It requires two named parameters:
  • label : what is shown to the user
  • value : what you will get back if the user chooses this option
final options = <Option>[
  new Option(label: 'I want Red', value: '#f00'),
  new Option(label: 'Gimme Blue', value: '#00f'),
  new Option(label: 'GREEN!', value: '#0f0'),
];
  1. Use the askMultiple() method on your instance of Prompter() to ask a question. Dont forget to assign the result of calling the method to a variable. The askMultiple() method takes two parameters and returns a String:
  • String prompt : The question shown to the user
  • List< Option > options : A list of the options
final String colorCode = prompter.askMultiple('What is your favorite color?', options);
  1. You can now use the result you got back in your variable and do whatever you wish with it: (for example printing it on the screen)
print(colorCode);

Asking a yes-no/binary question:

  1. Create a new instance of Prompter() class
final prompter = new Prompter();
  1. Use the askBinary() method on your instance of Prompter() to ask a question. Dont forget to assign the result of calling the method to a variable. The askBinary() method takes one parameter, and returns a boolean:
  • String prompt : The question shown to the user
final bool answer = prompter.askBinary('Do You like dragons?');
  1. You can now use the result you got back in your variable and do whatever you wish with it: (for example printing it on the screen)
print(answer);

Example

Below is a complete usage example, for both multiple-choice and yes-no questions:

//Importing the package:
import 'package:simple_prompter/simple_prompter.dart';

void main() {
//Creating a new instance of Prompter():
  final prompter = new Prompter();

//Creating a bunch of options for colors and their hex codes:
  final options = <Option>[
    new Option(label: 'I want Red', value: '#f00'),
    new Option(label: 'Gimme Blue', value: '#00f'),
    new Option(label: 'GREEN!', value: '#0f0'),
  ];

//Asking a multiple-choice question:
  final String colorCode = prompter.askMultiple('What is your favorite color?', options);

//Asking a yes/no question:
  final bool answer = prompter.askBinary('Do You like dragons?');

//Printing the 'value' of the selected color:
  print(colorCode);

//Printing true for 'y'/yes and false otherwise:
  print(answer);
}

After running the above code in Terminal/Commandline:

  1. First the screen will clear up, then the question/prompt(along with the options if it is multiple choice) will be shown to the user, waiting for them to answer:
$ What is your favorite color?
$ (1) - I want Red
$ (2) - Gimme Blue
$ (3) - GREEN!
$
$ Enter your answer
$ >
  1. Let's say the user types '2' and hits enter. They will then be shown this:
$ Do You like dragons? (y/n)
$
$ Enter your answer
$ >
  1. Let's assume they type 'yes' then hit enter:
$ Do You like dragons? (y/n)
$
$ Enter your answer
$ >yes
$ #00f
$ true

As is evident, their selected answers are printed to the screen.

Return Values

For multiple-choice questions, the return value is the 'value' associated with that option. For yes/no questions, the return value will be true if user types anything that contains 'y' or 'Y' (for example: yes, yeeeee, y, YEEES, Yes, helloY, cowboy, ...) and false otherwise.

Error Handling

In multiple-choice questions, if any of the below scenarios happen, the user will be asked the question again:

  • if the user provides either an integer that doesn't correspond to any option (for example '5' in a 4-choice question)
  • if the user provides anything other than solely integers (for example: choice1, hello, etc)

Tributes

Tributes to the incredible teacher Stephen Grider, since without his phenomenal work this package wouldn't have been possible.

Created by Dr. Vala Sebt .

Libraries

simple_prompter