simple_prompter 2.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

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 .

2.2.1 #

  • Updated readme file
  • Updated example/main.dart file for a better example

2.1.1 #

  • Updated pubspec.yaml file

2.1.0 #

  • Updated pubspec.yaml file for newest Dart releases

2.0.0 #

  • Corrected the indexing of multiple-choice questions
  • Updated pubspec.yaml file
  • Added readme
  • Added changelog

1.0.0 #

  • The preliminary version which included the basic functions.
  • Added the error handling and nice styling.

example/main.dart

//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);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  simple_prompter: ^2.2.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:simple_prompter/simple_prompter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Jul 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Analysis suggestions

Package not compatible with runtime flutter-web on web

Because of the import of dart:io via the import chain package:simple_prompter/simple_prompter.dartpackage:simple_prompter/src/prompter.dartpackage:simple_prompter/src/terminal.dartdart:io

Package not compatible with runtime web

Because of the import of dart:io via the import chain package:simple_prompter/simple_prompter.dartpackage:simple_prompter/src/prompter.dartpackage:simple_prompter/src/terminal.dartdart:io

Health issues and suggestions

Document public APIs. (-1 points)

9 out of 9 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0