DeveloperBreeze

Tutorials Programming Tutorials, Guides & Best Practices

Explore 149+ expertly crafted tutorials tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.

Crafting Beautiful CLI Tools with Node.js: Make Command-Line Interfaces Fun

Tutorial August 20, 2024
javascript nodejs

To make your CLI tool more interactive, you can use inquirer to prompt users for input:

const inquirer = require('inquirer');

yargs.command({
    command: 'create',
    describe: 'Create a new file',
    handler() {
        inquirer.prompt([
            {
                type: 'input',
                name: 'filename',
                message: 'Enter the file name:'
            },
            {
                type: 'list',
                name: 'extension',
                message: 'Choose a file extension:',
                choices: ['.js', '.txt', '.md']
            }
        ]).then(answers => {
            console.log(chalk.blue(`Creating file: ${answers.filename}${answers.extension}`));
            // Add your file creation logic here
        });
    }
});

yargs.parse();