cli-questionnaire
v1.0.0
Published
A CLI questionnaire tool for interactive prompts.
Downloads
5
Maintainers
Readme
CLI Questionnaire 🎯
A CLI tool built with TypeScript for creating interactive questionnaires.
Table of Contents 📚
Installation ⚙️
From npm (once published)
You can install the package globally using npm:
npm install -g cli-questionnaireOr use it directly with npx:
npx cli-questionnaireFrom Source 🛠️
Clone the repository:
git clone https://github.com/elangendoen/cli-questionnaire.git cd cli-questionnaireInstall dependencies:
npm installBuild the project:
npm run build
Usage 🚀
Using the CLI
Once installed globally or via npx, you can run the CLI tool:
cli-questionnaireProgrammatic Usage 🖥️
You can also use the Prompt function programmatically in your TypeScript or JavaScript projects:
import { Prompt, Question } from 'cli-questionnaire';
const questions: Question[] = [
{
id: 'q1',
type: 'multiple-choice',
question: 'What is your favorite programming language?',
options: ['JavaScript', 'TypeScript', 'Python'],
},
{
id: 'q2',
type: 'open',
question: 'What is your name?',
allowBackNavigation: true,
},
{
id: 'q3',
type: 'number',
question: 'How many years of experience do you have in programming?',
condition: (answers) => {
const q1Answer = answers.find((a) => a.id === 'q1')?.answer;
return q1Answer === 'TypeScript';
},
},
];
(async () => {
const answers = await Prompt({
questions,
allowBackNavigation: true,
allowSkip: true,
});
console.log('Your answers:', answers);
})();Question Properties 📝
Each question in the questions array can have the following properties:
id(required): A unique identifier for the question.type(required): The type of question. Can be one of:'multiple-choice': A question with predefined options.'open': A free-text question.'number': A question expecting a numeric answer.
question(required): The text of the question to display to the user.options(required for'multiple-choice'): An array of strings representing the available options.allowBackNavigation(optional): A boolean indicating whether the user can navigate back to this question. Defaults tofalse.allowSkip(optional): A boolean indicating whether the user can skip this question. Defaults tofalse.condition(optional): A function that determines whether this question should be asked. The function receives the currentanswersarray and should returntrueorfalse.
Question Types with Examples 🛠️
Here are examples of the different question types supported by the Prompt function:
Multiple-Choice Question
A question with predefined options that the user can select from:
{
id: 'q1',
type: 'multiple-choice',
question: 'What is your favorite programming language?',
options: ['JavaScript', 'TypeScript', 'Python', 'Java'],
}Open Question
A free-text question where the user can type their answer:
{
id: 'q1',
type: 'open',
question: 'What is your name?',
}Number Question
A question expecting a numeric answer:
{
id: 'q1',
type: 'number',
question: 'How many years of experience do you have in programming?',
}Build 🏗️
To compile the TypeScript code to JavaScript, run:
npm run buildThe compiled files will be output to the dist directory.
Testing 🧪
Run the tests using Jest:
npm testTo generate a coverage report:
npm run coverageLicense 📜
This project is licensed under the ISC License.
