mander
v1.1.0
Published
A simplified version of commander.js with automatic interactive mode and i18n support
Maintainers
Readme
mander
A simplified version of commander.js with automatic interactive mode and internationalization support (Chinese/English).
Features
- Intuitive command line parsing
- Support for subcommands
- Option definitions (short and long options)
- Automatic help generation
- Version information
- Interactive mode when no arguments are provided
- Internationalization support (Chinese (zh) and English (en)), default to English
Installation
npm install mander
Basic Usage
import { mander } from 'mander';
// Create a command with default language (English) const program = mander('myapp') .description('A sample command line tool') .version('1.0.0') .option('-d, --debug', 'Enable debug mode', false) .option('-o, --output ', 'Output file path', 'output.txt');
// Create a command with Chinese language const chineseProgram = mander('myapp', 'zh') .description('一个示例命令行工具') .version('1.0.0') .option('-d, --debug', '启用调试模式', false) .option('-o, --output ', '输出文件路径', 'output.txt');
// Add a subcommand
program.command('create', 'Create a new file')
.arguments(' [template]')
.option('-t, --type ', 'File type', 'txt')
.action((options, filename, template) => {
console.log(Creating ${filename}.${options.type});
// Your logic here
});
// Change language dynamically program.language('zh');
// Parse arguments program.parse();
API
mander(name: string, language?: 'zh' | 'en')
Create a new command instance with the given name and optional language. Default language is English ('en').
.description(text: string)
Set the description for the command.
.version(version: string)
Set the version number for the command. When set, the -v and --version options are automatically added.
.language(lang: 'zh' | 'en')
Set the language for the command and all its subcommands.
.option(flags: string, description: string, defaultValue?: any)
Define a command option. Flags can be a combination of short and long options (e.g., -d, --debug).
.command(name: string, description: string)
Create a new subcommand with the given name and description.
.arguments(desc: string)
Define the arguments for the command. Use <arg> for required arguments and [arg] for optional arguments.
.action(callback: (...args: any[]) => void | Promise)
Set the action callback to be executed when the command is invoked. The callback receives the parsed options followed by the arguments.
.parse(args?: string[])
Parse the command line arguments. If no arguments are provided, it uses process.argv.slice(2). If no arguments are present, interactive mode is triggered.
Interactive Mode
When no command line arguments are provided, mander automatically enters interactive mode, guiding the user through:
- Selecting a command (if subcommands exist)
- Providing required arguments
- Setting options
The interactive prompts will be displayed in the current language setting.
Internationalization
Mander supports both Chinese (zh) and English (en):
- Default language is English ('en')
- You can specify language when creating a command:
mander('myapp', 'zh') - You can change language dynamically with
.language('zh')
All built-in messages, help information, and interactive prompts will be displayed in the selected language.
License
MIT
