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

cuisinart

v0.1.4

Published

A node js command line parser

Downloads

2

Readme

cuisinart

Build Status

A node js command line parser, built for mise.js

cuisinart is used for parsing command line arguments with modular subcommands. It will auto generate help based on the subcommands available.

Programs

You can create a program by calling cuisinart.program():

var cuisinart = require('cuisinart');
// you must give your program a name
var program = cuisinart.program('app');

program
  .description('Does nothing.')
// after setting up your app, process arguments to run the program.
  .parse(process.argv);

You can add commands to cuisinart by calling .command() on your program. You can also add usage by calling .usage(). You can add any number of commands, they will be executed and listed in order.

var cuisinart = require('cuisinart');
var program = cuisinart.program('app');

program
  .version('0.0.0')
  .usage('[command] [options] ...')
  .description('an app with some commands and options')
  .command(commandOne)
  .command(commandTwo)
  .parse(process.argv);

This will automatically generate the --version and --help commands, and print the following when running without args or with --help:

Usage: app [command] [options] ...
       an app with some commands and options

  -h, --help  prints this help.
  -v, --version  prints the version.

Commands:

  app command1 - first command.

    --o1  sets option one.

  app command2 - second command.

    --o2  sets option two.

You can also add default arguments that will be passed through after the options hash to command run methods:

var cuisinart = require('cuisinart');
var program = cuisinart.program('app');

program
  .version('0.0.0')
  .usage('[command]')
  .description('an app with default arguments')
  .command(commandOne)
  .baseArgs('one','two')
  .parse(process.argv);

The arguments will then be added to the arguments of the run call, and can be received like so:

var commandOne = {
  name : 'commandOne',
  run : function(options,arg1,arg2){
    // arg1 is 'one',arg2 is 'two'
  }
}

All of the default commands are synchronous, but if you have async commands, you can pass a callback to the .parse call:

program
// setup program...
  .parse(process.argv,function(err){
    // this will be executed after all commands have run, and will return any errors they call back with
  });

Commands

A command is just an object with some mandatory keys. cuisinart asserts the keys, so it'll throw errors if you don't specify something mandatory.

var command = {
  options : [
    {
      name : 'foo',
      flag : 'f',
      description : 'sets foo value'
    },{
      name : 'bar',
      longFlag : 'bar',
      description : 'if called with bar flag, bar will be true.'
    }
  ],
  root : false,
  name : 'complex',
  description : 'a complex command.',
  run : function(options){
    if(options.foo){
      console.log('foo is ' + options.foo);
    }
    console.log('bar enabled : '+(options.bar ? 'yes' : 'no'));
  }
};

Commands can be synchronous or async. Cuisinart expects that if a command is async, the callback will be called, invoked, or applied by the same name that it is passed to the run method of the command.

Examples

var command = {
  name : 'async',
  run : function(options,baseArg,callback){
    setTimeout(function(){
      callback();
      // note that this can be called with any arguments, and can also be:
      // callback.call()
      // or
      // callback.apply()
    },1000);
  }
}

However, cuisinart is unable to detect that a command is async if you rename the callback or access it directly from the arguments list. So this example would be run synchronously, even though it is supposed to be async:

var command = {
  name : 'bad-async',
  run : function(options,baseArg,callback){
    var args = Array.prototype.slice.call(arguments);
    var done = args.pop();
    setTimeout(function(){
      done();
      // because we're calling our callback by a different name, cuisinart is not able to detect that it should wait for this command.
      // as a result, this would accidentally call the next command before this one completes.
    },1000);
  }
}

There are 6 possible keys for a command, 2 of which are mandatory:

  • name (mandatory) : this is the name of the command. Use this name to invoke the command.

example:

app complex
  • run (mandatory) : this is a method that will be called when a command is run. The options hash will be passed to this function.

  • options (optional) : you can specify options if you have them, these will be parsed and set either as true/false or with their values and passed to the run function

example:

app complex -f value --bar
  • description (optional) : this is printed with the command when --help is called.

  • root (optional) : this is used to indicate that this command does not need to be called explicitly - --help and --version are examples of root commands. They are essentially option-only commands. In normal cases, you would not specify this at all, as it defaults to false.

  • stop (optional) : if this is set to true, it will prevent other commands from running after this command.

Notes

Bugs/PRs are welcome, file them here in the github issues.