DEV Community

Discussion on: Make and publish a NodeJS CLI in 10 minutes!!

Collapse
 
andrewbridge profile image
Andrew Bridge

Nice! I find making Node CLIs to be a good way of keeping things cross-platform friendly while still providing the flexibility of the command line.

For more complex projects, I use Commander.js which does a lot of the boilerplate stuff (like parsing arguments and flags) for you.

It allows you to write quite neat code like:

program
   .version('0.0.1')
   .option('-C, --chdir <path>', 'change the working directory')
   .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
   .option('-T, --no-tests', 'ignore test hook')

 program
   .command('setup')
   .description('run remote setup commands')
   .action(function() {
     console.log('setup');
   });

 program
   .command('exec <cmd>')
   .description('run the given remote command')
   .action(function(cmd) {
     console.log('exec "%s"', cmd);
   });
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aadityasiva profile image
Aadityasiva • Edited

Thanks for sharing really appreciate it