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

mini-cli

v1.3.0

Published

Maps command line arguments to actions.

Downloads

35

Readme

mini-cli

Build Status

Simple cli command to action mapper.

Yes there is a huge amount of those already but why do they all have to be so complicated... I just need a way to call a function when a certain string is in the arguments, with just a tad of fluff.

Usage

var MiniCli = require('mini-cli');
var cli = new MiniCli();

cli.command('cmd');
cli.option('c', 'x');
cli.action(function (context, args, options) {
    // execute 'cmd' command
    return '"cmd" command result';
});

cli.command('read');
cli.action(function (ctx, args, options) {
    return 'contents at ' + args[0];
});

let result = cli.parse(process.argv.slice(2));
console.log(result);

Api

MiniCli.constructor

Create new cli programme.

MiniCli.command(string: name)

Start new command map. All following method calls would relate to this command instance.

MiniCli.description(string: description)

Set human readable description.

MiniCli.alias(string: name)

Makes an association to the current command map by the different name.

MiniCli.args(...string)

List of arguments(s) expected by this command. Last argument can be a function(ctx, value) which will be called for each encountered argument. Any return value which evaluates to true will break execution and cause MiniCli.parse to return it at that moment (no other arguments will be processed);

Argument definition format: <argument name>[=default]

Defining arguments will cause MiniCli.action function to receive arguments hash { _:[], {string:string} } pairs instead of string[].

Note: "_: []" - contain arguments not matched by args() function;

E.g.
var a = cli.command('a').action(function (ctx, args) {
  console.log(args instanceof Array); // true
});

var b = cli.command('b').args('first').action(function (ctx, args) {
  console.log(args); {_: {}, first: 'foo'}
})

MiniCli.option(...string)

List of string option(s) expected by this command. Last argument can be a function(ctx, value) which will be called for each encountered argument. Any return value which evaluates to true will break execution and cause MiniCli.parse to return it at that moment (no other arguments will be processed);

Option argument format: [!]<option name>[=default]

  • ! - specifies that the option is required. MiniCli.parse would return an error object ignoring before invoking MiniCli.action callback.
  • <option name> - name of the option argument
  • =default - default value
E.g.
cli.option('c', '!mustHave', 'x=foo' function () {
    // 'c', 'x' or 'mustHave' are present in the
    //arguments to the programme
});

MiniCli.action(function: callback)

  • function(object: context, array: args, object: options);

Register a callback function for the command.

  • context - created new and destroyed for each request. use it for whatever
  • args - list of arguments
  • options - object with option values

MiniCli.parse(array: args[, object: context])

Invoked an callback for the respective MiniCli.action callback. It's return value is whatever action callback returns.

It is possible to provide custom context object command action callbacks using second argument.

[static] MiniCli.parse(array: args, function: callback)

Helper function to create and parse arguments array in a single step.

MiniCli.commands()

Returns Iterator.<id, name, description> over defined commands.

  • id - Command string match. Value given to a command trigger via command or alias methods.
  • name - Command name, this would match to the value of command method.
  • description - Value matching command description.