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

readcommand

v0.4.0

Published

An extension to readline that reads and parses a multi-line shell command

Downloads

88

Readme

ReadCommand

Build Status

A utility that wraps the built-in node.js readline functionality to parse multi-line shell commands. This module was extracted from work that was bloating in node-corporal, a utility that makes building CLI apps FUN.

Features

  • Argument parsing from the user's input
  • Multi-line command input
  • Command history navigation using up and down arrow keys
  • Auto-complete hook for argument auto-completion

API

Examples

Read a single command

This is the most simple example, reading a command from a user in your interactive shell, and parsing it into an arguments array like process.argv, then printing it to the console.

require('readcommand').read(function(err, args) {
    console.log('Arguments: %s', JSON.stringify(args))
});

Example usage:

$ node simple.js
> mycommand --arg1 val1 --arg2 val2
Arguments: ["mycommand","--arg1","val1","--arg2","val2"]

And multi-line goodness!

$ node simple.js
> mycommand \
>   --arg1 val1 \
>   --arg2 val2
Arguments: ["mycommand","--arg1","val1","--arg2","val2"]

Read a single command with history

Provide a history array for the user to toggle through using up/down keys.

require('readcommand').read({'history': ['cd', 'ls', 'curl', 'ps -aux | grep node', 'netstat -r']}, function(err, args) {
    console.log('Arguments: %s', JSON.stringify(args))
});

Start a command loop

This example starts an interactive command prompt, where each command is provided to the callback function. After 2 SIGINT invocations by the user, the loop terminates. After each command, it prints the parsed arguments. The command history is maintained within the command loop.

var readcommand = require('readcommand');

var sigints = 0;

readcommand.loop(function(err, args, str, next) {
    if (err && err.code !== 'SIGINT') {
        throw err;
    } else if (err) {
        if (sigints === 1) {
            process.exit(0);
        } else {
            sigints++;
            console.log('Press ^C again to exit.');
            return next();
        }
    } else {
        sigints = 0;
    }

    console.log('Received args: %s', JSON.stringify(args));
    return next();
});

Auto-complete

The auto-complete functionality wraps readline's autocomplete functionality, providing an API that works with high-level parsed argument replacement instead of low-level loose string replacement.

The following example provides replacements for either argument keys or argument values depending on context:

var _ = require('underscore');
var readcommand = require('readcommand');

var possibleArguments = [
    '--verbose',
    '--log-level'
];

var possibleLogLevels = [
    'trace',
    'debug',
    'info',
    'warn',
    'error'
];

readcommand.loop({'autocomplete': _autocomplete}, function(err, args, str, next) {
    if (err) {
        return;
    }

    console.log('executed: %s', JSON.stringify(args));
    return next();
});

function _autocomplete(args, callback) {
    if (_.isEmpty(args)) {
        return callback(null, possibleArguments);
    }

    var replacements = [];
    var lastArg = _.last(args);
    if (args.length === 1) {
        // If there is only one argument, we just try and auto complete one of the possible flags
        replacements = _.filter(possibleArguments, function(possibleArgument) {
            return (possibleArgument.indexOf(lastArg) === 0);
        });
    } else {
        var secondLastArg = args[args.length - 2];
        if (secondLastArg === '--log-level') {
            // If we are choosing a log level, we offer up potential log levels for the value
            replacements = _.filter(possibleLogLevels, function(possibleLogLevel) {
                return (possibleLogLevel.indexOf(lastArg) === 0);
            });
        } else {
            // We are not giving a flag value, so offer up another flag
            replacements = _.filter(possibleArguments, function(possibleArgument) {
                return (possibleArgument.indexOf(lastArg) === 0);
            });
        }
    }

    return callback(null, replacements);
}

License

Copyright (c) 2014 Branden Visser

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.