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

cli.args

v0.0.7

Published

Easy command line arguments handling library for node.js

Downloads

29

Readme

cli.args

NPM version Build Status

npm

Flexible and easy command line arguments handling library for your node.js applications.

Installation

npm install cli.args

Use

cli.args is a small and versatile library to help you easily support dynamic runtime behavior (based on command line input parameters) in your node.js apps.

For instance, starting a node server on a port that is specified at runtime via a command line option (for e.g. -p) is as simple as,

var args = require('cli.args')('p:');
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(args.p, '127.0.0.1');
console.log('Server running at http://127.0.0.1:'+args.p+'/');

which can be started from the command line as,

$ node app.js -p 8080
Server running at http://127.0.0.1:8080/

Furthermore, to ensure that the port number has to always be specified, the only change is an addition to the first line appending a '!' to the required option character (p in this case) as shown below,

var args = require('cli.args')('p:!');

Description

cli.args uses an option string (based on the getopt() style option string) to parse supported command line parameters, and returns an easy to use JSON object of the parsed arguments along with some useful extra information.

The option string may be specified in either a short (POSIX like) option string or a long (GNU long) format options array. Short options are normally specified by a preceding dash / hyphen character (-) on the command line, while long options are preceded by double dashes (--).

Option String

  • Short format

    In this format, all the valid command line options that the application wants to support are specified in a single string, containing each valid option represented by a single alphanumeric character. Each option character is followed by either (OR both OR none) of the following 2 characters which affect the way the preceding option is parsed,

    • ':' - indicates that the preceding option character requires an argument
    • '!' - indicates that the preceding option character is mandatory
    var args = require('cli.args')('a'); // the application supports an option named 'a' without an argument
    var args = require('cli.args')('a!'); // indicates that 'a' is a mandatory option
    var args = require('cli.args')('a:'); // indicates that 'a' needs an argument
    var args = require('cli.args')('a:!'); // 'a' is a mandatory option that also requires an argument
  • Long format

    In this format, the valid command line options that the application wants to support are specified as an array of strings. Each valid option is specified as a member of the array followed by a ':' or '!' character (OR both OR none), to affect the way the option is parsed (similar to the short format).

    // An example of an app that supports 2 long format options (--port, --debug),
    // and a short format option -a (single character elements are considered short).
    // The option --port requires an argument and is also a mandatory option for this app.
    var args = require('cli.args')(['port:!', 'debug', 'a']);

Resulting object

The parsed arguments are returned as a JSON object. For example consider the following,

var args = require('cli.args')('p:!su');

When used from the command line with your node application (app.js) in the following way,

$ node app.js -u -p 8080 nonOptionalArg1

results in the object args containing the following values,

{
  u: true,   // the -u option with its value (true as it does not require an argument)
  p: '8080', // the -p option with its value
  nonOpt: [ 'nonOptionalArg1' ], // all nonOptional cli arguments are available in this array
  info: {    // a generated object containing app usage information
    usage: 'node app.js -p value [-s] [-u]',
    summary: 'Usage: node app.js -p value [-s] [-u]'
  },
  argv: [    // the original process.argv array contents
    'node',
    '/home/meyn/workspace/playServer/app.js',
    '-u',
    '-p',
    '8080',
    'nonOptionalArg1'
  ]
}

Examples

  1. Handling a command line argument (-p) which requires a value,

    var args = require('cli.args')('p:');
    console.log('-p=' + args.p);

    use as,

    $ node app.js -p 8080; #output "-p=8080"
  2. Handling multiple arguments (-p, -s, -u) with some requiring values (-p),

    var args = require('cli.args')('p:su');
    console.log('-p=' + args.p + ', -s set: ' + (args.s ? 'y' : 'n') + ', -u set: ' + (args.u ? 'y' : 'n'));

    use as,

    $ node app.js -s -p 8080; #outputs "-p=8080, -s set: y, -u set: n"
  3. Specifying required arguments,

    try {
        var args = require('cli.args')('p:!s');
        console.log('-p=' + args.p);
        console.log('-s set? ' + (args.s ? 'y': 'n'));
    } catch (e) {
        console.error('Command error:', e.message);
    }

    when used without the required option (-p),

    $ node app.js -s; #throws an Error for missing required argument 'p'
  4. Handling long-format arguments,

    var args = require('cli.args')(['port:', 'debug']);
    console.log('--port=' + args.port);
    console.log('--debug? ' + (args.debug ? 'y': 'n'));

    use as,

    $ node app.js --port 8080; #outputs "--port=8080"
                               #        "--debug? n"
    $ node app.js --port 8080 --debug; #outputs "--port=8080"
                                       #        "--debug? y"

Tests

npm test; # requires mocha