npm

@fnet/prompt

0.2.14 • Public • Published

@fnet/prompt

The @fnet/prompt project offers a straightforward way to gather user input through command-line prompts. Built on top of the Enquirer library, it simplifies the process of defining and collecting responses, making it useful for developers who need a quick and easy solution for interactive command-line applications.

How It Works

This project leverages the Enquirer library to present prompts to users in the command line. By defining the prompts in an array or object, users can specify the type and name of each prompt. If not provided, default values are assigned. Once configured, the prompts are displayed, and the user's input is collected for further processing.

Key Features

  • Type Flexibility: Automatically assigns a default type of 'input' if not specified, ensuring a smoother user experience.
  • Dynamic Prompt Naming: Generates default names for prompts, allowing for consistent identification of responses.
  • Enquirer Integration: Utilizes the Enquirer library to manage and display prompts, offering reliability and ease of use.

Conclusion

The @fnet/prompt project serves as a helpful tool for developers when user input is needed in command-line applications. Its integration with Enquirer provides a simple and reliable interface, handling user prompts effectively with minimal setup.

Developer Guide for @fnet/prompt

Overview

The @fnet/prompt library is designed to simplify the process of gathering user input in Node.js applications. By leveraging the enquirer module, it allows developers to easily create interactive command-line prompts. The library automates setting up default configurations for user prompts, making it easier to gather input with minimal setup.

Installation

To install the library, use either npm or yarn:

npm install @fnet/prompt

or

yarn add @fnet/prompt

Usage

The primary functionality of @fnet/prompt is encapsulated in a default exported function. This function takes an argument or an array of arguments to specify the prompts you wish to display. Let's dive into how you can use it in your projects.

Basic Usage

Here is a basic example of how to use the library:

import prompt from '@fnet/prompt';

async function gatherInput() {
    // Define the prompt questions
    const questions = [
        {
            type: 'input',
            name: 'username',
            message: 'What is your username?'
        },
        {
            type: 'password',
            name: 'password',
            message: 'Enter your password'
        }
    ];

    // Execute the prompt
    const responses = await prompt(questions);

    console.log('User Responses:', responses);
}

gatherInput();

Single Prompt

For a single prompt, you can pass an object directly:

import prompt from '@fnet/prompt';

async function singlePrompt() {
    const response = await prompt({
        type: 'input',
        message: 'Please enter your email:',
        name: 'email'
    });

    console.log('Email:', response.email);
}

singlePrompt();

Examples

Below are some example scenarios showcasing how to use @fnet/prompt for typical use cases:

Multiple Choice

To gather input for a multiple-choice question:

import prompt from '@fnet/prompt';

async function multipleChoicePrompt() {
    const response = await prompt({
        type: 'select',
        name: 'favoriteColor',
        message: 'What is your favorite color?',
        choices: ['Red', 'Green', 'Blue']
    });

    console.log('Selected Color:', response.favoriteColor);
}

multipleChoicePrompt();

Confirm Prompt

Prompting the user for a yes/no confirmation:

import prompt from '@fnet/prompt';

async function confirmAction() {
    const response = await prompt({
        type: 'confirm',
        name: 'confirm',
        message: 'Do you want to proceed?'
    });

    console.log('Confirmed:', response.confirm);
}

confirmAction();

Acknowledgement

This library utilizes the enquirer library, ensuring a robust and flexible way to create prompts for user input in command-line applications.

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
title: PromptArguments
type:
  - array
  - object
description: Schema for prompt arguments used in Enquirer prompts.
anyOf:
  - type: array
    items:
      $ref: "#/$defs/promptItem"
  - $ref: "#/$defs/promptItem"
$defs:
  promptItem:
    type: object
    properties:
      type:
        type: string
        default: input
        description: Type of the prompt (e.g., input, checkbox).
      name:
        type: string
        default: user_input
        description: Name identifier for the prompt input.

Readme

Keywords

none

Package Sidebar

Install

npm i @fnet/prompt

Weekly Downloads

60

Version

0.2.14

License

MIT

Unpacked Size

13.8 kB

Total Files

6

Last publish

Collaborators

  • serdark
  • serdar986
  • gboyraz