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

task-yargs

v0.1.5

Published

Create sub-task oriented command line interfaces with yargs easily

Downloads

24

Readme

task-yargs

Create sub-task oriented command line interfaces with yargs easily.

NPM

Build Status Coverage Status

What is "sub-task oriented"?

Some programs have a command line interface which exposes a single global interface. Great examples of it are:

  • ls
  • tail

However, some programs are complex enough, such that they perform several distinct tasks. As a result, the set of flags and options varies greatly from one task to another. Great examples are:

  • npm
    • e.g. npm init, npm publish
  • git
    • e.g. git clone, git commit

These latter programs are "sub-task oriented".

yargs

yargs is a great NodeJs module for parsing command line arguments. It exposes a fluent interface, and exposes a no-nonsense means of defining and querying the various flags.

For programs which expose a single global command line interface, it is perfect. One shortcoming it has, however, it that it does not allow you to define and manage multiple sub-tasks. This is where task-yargs comes in.

Prerequisite tasks

One of the main caveats encountered during the building of multiple instances of yargs within the same NodeJs program, is that several different sub-tasks will have various flags and options in common with each other. There needs to be an easy way for one task to say that it would like to use the flags and options that have been defined in a another task, without having to repeat or redefine them This module calls these "prerequisite tasks".

Prerequisite tasks help to keep your yargs definitions DRY.

When a sub-task defines one or more of the other sub-tasks as its prerequisite tasks, all the yargs checks and options defined, recursively, by its prerequisite tasks are defined upon itself automatically. This allows one to adhere to DRY principles in defining each of the sub-tasks.

Usage

In your project directory: npm install task-yargs --save

Entry Point

In the entry point for your project - usually index.js or bin/cli.js, import task-yargs:

var taskYargs = require('task-yargs');

... and you are good to go with the task-yargs API.

To do something useful, you might use it like so, also in the same entry point file:

var cliArgs;
var taskName = taskYargs.getCurrentName();
if (taskName) {
  var yargsInstance = taskYargs.getCurrent();
  cliArgs = yargsInstance.argv;
  // assuming that all your subtasks expose a `--help` flag
  if (cliArgs.help) {
    yargsInstance.showHelp();
  }
  else {
    // Run the subtask by the name of `taskName`
    // e.g. When using gulp, use `run-sequence` to invoke the appropriate gulp task
    require('run-sequence')(taskName);
  }
}
else {
  // Display an error message as no recognised subtask was specified
}

Querying the CLI Parameters

Of course, once you invoke the function for the sub task, you will need to respond to it, which involves appropriately identifying the command line parameters.

function fooTask() {
  // `taskYargs.getCurrent()` return a regular `yargs` instance
  // so simply use it as you would any other `yargs` instance
  var yargsInstance = taskYargs.getCurrent();
  yargsInstance.strict().wrap(100);
  var argv = yargsInstance.argv;

  // Now do stuff based on the `argv` object
}

Register

The above will not work unless task-yargs has been registered with a task. To do so, pass the same instance as the one used in the entry point file, to wherever you wish to register it. This could be anywhere, but it makes the most sense to place it in either:

  • The entry point file
  • The file containing the function responding to that subtask
taskYargs.register('foo', {
  description: '"foo" does blah blah',
  prerequisiteTasks: ['bar'], // be sure to register a `bar` task elsewhere
  options: [
    // The `key` and `value` are passed to `yargs.option(key, value)`
    {
      key: 'baz',
      value: {
        describe: 'Set value of baz',
        alias: ['b'],
        string: true,
        default: false
      }
    }
  ],
  checks: [
    // A check function to pass to `yargs.check()`
    function checkFoo(argv) {
      if (argv.help) {
        return true;
      }
      else if (argv.baz === 'illegalValue') {
        throw new Error('Value of baz option is illegal');
      }
      else {
        return true;
      }
    }
  ]
});

Contributing

This repository uses the git flow branching strategy. If you wish to contribute, please branch from the develop branch - pull requests will only be requested if they request merging into the develop branch.

Author

Maintained by Brendan Graetz

bguiz.com

Licence

GPLv3