Welcome to Your Comprehensive Guide to Sade
Sade is a minimal but powerful CLI framework for building complex CLI tools in Node.js. In this blog post, we’ll provide an introduction to Sade, share dozens of useful API examples with code snippets, and demonstrate how you can use these APIs in a sample app.
Getting Started with Sade
To get started with Sade, you first need to install it:
npm install sade
Creating a Basic CLI
Creating a basic CLI with Sade is straightforward. Here’s a simple example:
const sade = require('sade');
const prog = sade('myapp')
.version('1.0.0')
.option('--global, -g', 'A global option');
prog.command('build', 'Build your project')
.option('--output, -o', 'Output directory', 'dist')
.action(opts => {
console.log('Building project...');
});
prog.parse(process.argv);
Defining Commands
In addition to the base command, you can define additional commands using the command
method:
prog.command('deploy', 'Deploy your project')
.option('--env, -e', 'Specify environment', 'production')
.action(opts => {
console.log(`Deploying to ${opts.env} environment...`);
});
Advanced Options Handling
Sade allows you to define commands with both mandatory and optional arguments:
prog.command('serve [dir]', 'Serve your project')
.option('--port, -p', 'Port to listen on', 3000)
.action((dir, opts) => {
console.log(`Serving directory ${dir} on port ${opts.port}...`);
});
Combining Commands and Options
You can create a complex CLI tool by combining multiple commands and options:
prog.command('init ', 'Initialize a new project')
.option('--template, -t', 'Template to use')
.action((name, opts) => {
console.log(`Initializing project ${name} with template ${opts.template}...`);
});
prog.command('test')
.description('Run tests')
.option('--watch, -w', 'Watch files for changes')
.action(opts => {
console.log(`Running tests in watch mode: ${opts.watch}`);
});
prog.command('lint')
.description('Lint your code')
.action(() => {
console.log('Linting code...');
});
// Parse the arguments prog.parse(process.argv);
Sample Application Using Sade
Here’s a complete example of a Node.js CLI application built with Sade:
const sade = require('sade'); const fs = require('fs'); const path = require('path');
const prog = sade('file-cli').version('1.0.0');
prog.command('read ', 'Read a file')
.action((file) => {
const filePath = path.resolve(file);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file: ${err.message}`);
} else {
console.log(data);
}
});
});
prog.command('write ', 'Write to a file')
.action((file, content) => {
const filePath = path.resolve(file);
fs.writeFile(filePath, content, 'utf8', (err) => {
if (err) {
console.error(`Error writing to file: ${err.message}`);
} else {
console.log(`Content written to ${file}`);
}
});
});
prog.parse(process.argv);
With these examples, you have a robust understanding of how to use Sade to create your own CLI tools. By experimenting with the different APIs and combining commands and options, you can build complex and user-friendly command-line applications.
Happy coding!
Hash: 625c6bbd24b302543e3ad9161a948d579c85eceaf737696e5b0af55bcdbed445