Node.js

Subcommander 1.0.0

After almost 3 years after the initial commit, I have finally released subcommander v1.0.0.

Even though it sounds like yet another command-line argument parser for Node.js, subcommander is a bit different. Supporting multiple sub-command levels, it can help you create a rich CLI for your Node.js application.

A bit of history

Back in 2014 I was working on Bender.js – a bespoke test runner for CKEditor 4. One of its features was a pretty neat command line interface with multiple commands, sub-commands and plenty of parameters assigned to them. To make it all possible, I needed something more powerful than the famous commander by TJ Holowaychuk, a module which actually was an inspiration for the API subcommander exposes.

The biggest difference between those two modules is command nesting. While commander allows a single level of sub-commands, in subcommander there’s no limit.

Example CLI

To give an example let’s consider implementing the following CLI structure:

  • example (our executable)
    • init
    • configure
    • server
      • start
      • stop
      • restart
      • status
        • connections
        • uptime
    • version

Here’s a code snippet for such a CLI:

Here’s how it looks when executed with -h flag (or --help, shows the usage information):

When we pass the server command and display usage information we get:

And finally, server status command and its usage info:

Notice the available options which were inherited from the parent command – server.

Final words

Subcommander started as a tiny Node module, which originally was meant to be used exclusively in my personal projects.

It’s really nice to see it became useful to some other folks too:

Subcommander NPM stats
Subcommander NPM stats

To find out more about the module, please visit subcommander’s github page, where you can find more examples, usage guide and detailed API documentation.

by Greg

No Comments »

Leave a Reply

Your email address will not be published. Required fields are marked *