npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

subcli

v0.2.4

Published

helpers for creating command-line interfaces that support subcommands

Downloads

18

Readme

subcli

Code Climate GPA Code Climate Issue Count Test Coverage

Helper utilities for creating command-line interfaces (CLIs) that support subcommands.

Right now, there is a parse function and a usage function.

Getting Started

  1. Install the module in your project

     $ npm install --save subcli

How to Use

To use the library, you'll need to create a command descriptor. This is a simple javascript object that describes your command and all of its subcommands. This command descriptor then gets passed to both the parse and usage functions.

By default, every command and subcommand includes a -h | --help option, but this can be overridden by passing {includeHelp: false} when calling parse or usage.

The parse command returns a JavaScript object, where the _ attributes stores the positional parameters. There may be a $ attribute that contains a sub-object that represents the positional parameters and options for the subcommand. The remaining attributes are just the named options and their values (true / false if they are boolean, and otherwise, whatever string was passed as the value).

You can optionally pass the results from the parse function to the usage function as its second argument. If you do so, subcli will be smart enough to determine whether or not a -h or --help command was passed, either for the parent command or for the subcommand. If it was, the usage message will be returned (for either the parent command or the subcommand as appropriate). Otherwise, it will be undefined. If you don't pass the return value from parse, then the usage message for the parent command will be returned.

Command Descriptor Attributes

The following attributes are supported in command descriptors:

  • name primary name of option
  • abbr one character alias of the option
  • alias other options treated as alias
  • boolean if true, the option is seen as a boolean flag
  • help usage string for the option
  • default default value of the option
  • commands nested subcommands, which also support this same list of attributes

It's worth noting that the attributes are an extension of cliclopts.

Example

import {parse, usage} from 'subcli'

// set up our command descriptor
const commandDescriptor = {
  name: 'act',
  description: 'act happy or sad',
  usage: 'act [options] [ <happy|sad> [options for happy or sad] ]',
  commands: [{
    name: 'happy',
    description: 'act happy',
    usage: 'happy [options]',
    options: [{
      name: 'gleeful',
      abbr: 'g',
      boolean: true,
      help: 'act really, REALLY happy',
    }],
  }, {
    name: 'sad',
    description: 'act sad',
    usage: 'sad [options]',
    options: [{
      name: 'miserable',
      abbr: 'm',
      boolean: true,
      help: 'act really, REALLY sad',
    }],
  }],
  options: [{
    name: 'be',
    abbr: 'b',
    boolean: true,
    help: "don't _act_, actually _be_",
  }],
}      

let args = parse(commandDescriptor, ['happy', '-g'])
// args =>
// { _: [ 'happy', '-e' ],
//   be: false,
//   b: false,
//   help: false,
//   h: false,
//   '$': { happy: { _: [], gleeful: true, g: true, help: false, h: false } } }

let usageMessage = usage(commandDescriptor, args)
// usageMessage => undefined
usageMessage = usage(commandDescriptor)
// usageMessage => the full help text for the act command

args = parse(commandDescriptor, ['-h', 'happy', '-g', '-h'])
// args =>
// { _: [ 'happy', '-g', '-h' ],
//   be: false,
//   b: false,
//   help: true,
//   h: true,
//   '$': { happy: { _: [], gleeful: true, g: true, help: true, h: true } } }
usageMessage = usage(commandDescriptor, args)
// usageMessage => the full help text for the act command

args = parse(commandDescriptor, ['happy', '-g', '-h'])
// args =>
// { _: [ 'happy', '-g', '-h' ],
//   be: false,
//   b: false,
//   help: false,
//   h: false,
//   '$': { happy: { _: [], gleeful: true, g: true, help: true, h: true } } }
usageMessage = usage(commandDescriptor, args)
// usageMessage => the full help text for the 'act happy' subcommand

See also the provided example. Or, run it like so: node example happy -g

Notes

It may help to look at minimist for more detail on how the argument parsing is handled. One thing to note is that, since our command parsing supports the notion of "subcommands", there may be an additional attribute named $ that contains the positional parameters and options for the subcommand, e.g.:

{
  _: [ 'happy' ],    // positional parameters for the parent command
  help: false,       // options for the parent command
  h: false,          // ...
  $: {               // subcommand
    happy: {
      _: [],         // positional parameters for the subcommand
      help: false,   // options for the subcommand
      h: false       // ...
    }
  }
}

Contributing

Read the instructions for contributing.

  1. Clone the repository.

  2. Run the setup tasks:

     $ npm install
     $ npm test

License

See the LICENSE file.