Why Use CLI Frameworks?
CLI frameworks provide structure, argument parsing, help generation, and plugin systems for building professional command-line tools.
bash
# Without framework
const args = process.argv.slice(2);
// Manual parsing, validation, help text
# With framework
program
.command('deploy <env>')
.option('-v, --verbose', 'verbose output')
.action(deploy);Popular Frameworks
Commander.js
typescript
import { Command } from 'commander';
const program = new Command();
program
.name('my-cli')
.description('My awesome CLI')
.version('1.0.0');
program
.command('deploy <env>')
.description('Deploy to environment')
.option('-v, --verbose', 'verbose output')
.action((env, options) => {
console.log(`Deploying to ${env}`);
if (options.verbose) console.log('Verbose mode');
});
program.parse();Yargs
typescript
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
yargs(hideBin(process.argv))
.command('deploy <env>', 'Deploy to environment', (yargs) => {
return yargs.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Verbose output'
});
}, (argv) => {
console.log(`Deploying to ${argv.env}`);
})
.parse();Oclif
typescript
import { Command, Flags } from '@oclif/core';
export default class DeployCommand extends Command {
static description = 'Deploy to environment';
static flags = {
verbose: Flags.boolean({ char: 'v' })
};
static args = [{
name: 'env',
required: true,
description: 'Environment to deploy'
}];
async run() {
const { args, flags } = await this.parse(DeployCommand);
console.log(`Deploying to ${args.env}`);
}
}Interactive CLIs
Ink (React for CLIs)
typescript
import React from 'react';
import { render, Text } from 'ink';
const App = () => (
<Text color="green">Hello CLI!</Text>
);
render(<App />);Best Practices
- 1Clear command structure
- 2Helpful error messages
- 3Auto-generated help text
- 4Consistent flags/options
- 5Shell completion support
Conclusion
CLI frameworks simplify building professional command-line tools with proper argument parsing, help generation, and error handling.
Choose the right framework and build great CLIs!