JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1753662
  • Score
    100M100P100Q189497F
  • License MIT

Simple command line prompting utility

Package Exports

  • promptly

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (promptly) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

promptly


Simple command line prompting utilify.

API

In all the commands, the options argument is not mandatory.

.prompt(message, opts, fn)

Prompts for a value, printing the message and waiting for the input.
When done, calls fn with an error and value.

Default options:

{
    // The default value to assume. If not supplied, the input is mandatory
    'default': 'default value',
    // Automatically trim the input
    'trim': true,
    // A validator or an array of validators.
    'validator': null,
    // Automatically retry on error
    'retry': false
}

The validators have two purposes:

function (value) {
    // Validation example, throwing an error when invalid
    if (value.length !== 2) {
        throw new Error('Length must be 2');
    }

    // Parse the value, modifying it
    return value.replace('aa', 'bb');
}

Example usages:

Ask for a name.

promptly.prompt('name: ', function (err, value) {
    if (err) {
        console.log('invalid name');
        // Manually call retry
        // The passed errors have a retry method to easily prompt again.
        return err.retry();
    }

    console.log(value);
});

Ask for a name until it validates (non-empty value).

promptly.prompt('name: ', { retry: true }, function (err, value) {
    console.log(value);
});

Ask for a name until it validates (non-empty value and length > 2).

var validator = function (value) {
    if (value.length < 2) {
        throw new Error('Min length of 2');
    }

    return value;
}

promptly.prompt('name: ', { retry: true, validator: validator }, function (err, value) {
    console.log(value);
});

.confirm(message, opts, fn)

Ask the user to confirm something.
Calls fn with an error and value (true or false).

The available options are the same, except that retry defauls to true.
Truthy values are: y, yes, 1 and true. Falsy values are n, no, 0 and false. Comparison is made in case insensitive way.

Example usage:

promply.confirm('Are you sure? ', function (err, value) {
    console.log('Answer: ', value);
});

.choose(message, choices, opts, fn)

Ask the user to choose between multiple choices (array of choices).
Calls fn with an error and value (true or false).

The available options are the same, except that retry defauls to true.

promply.choose('Do you want an apple or an orange? ', ['apple', 'orange'], function (err, value) {
    console.log('Answer: ', value);
});

.password(message, opts, fn)

Prompts for a password, printing the message and waiting for the input.
When available, calls fn with an error and value.

The available options are the same, except that trim defauls to false.

promply.password('password: ', function (err, value) {
    console.log('password is ' + value);
});

License

Released under the MIT License.