require('yargs') .usage('Usage: $0[arguments]') .help('h') .alias('h', 'help') .version(require(__dirname+'/package.json').version) .strict() .wrap(null) .demandCommand(1, 'Please choose a command') .command( 'create', "create something", (yargs) => { yargs.example('$0 create'); yargs.option('verbose', { alias: 'v', describe: 'print progress informations', }) }, function(argv) { console.log('success') } ) .argv;
create a file named : cli.js
#!/usr/bin/env node require('yargs') .commandDir('commands') .demandCommand() .help() .argv
create a file : commands/init.js
exports.command = 'init [dir]' exports.desc = 'Create an empty repo' exports.builder = { dir: { default: '.' } } exports.handler = function (argv) { console.log('init called for dir', argv.dir) }