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

options-parser

v0.4.0

Published

A full-featured no-nonsense command line parser with no external dependencies

Downloads

4,304

Readme

options-parser

A full-featured no-nonsense command line parser for node with no external dependencies

Installation

$ npm install --save options-parser

Example


var options = require('options-parser');

var result = options.parse({
  user:  { required: true },
  all:   { short: 'a', flag: true },
  host:  { short: 'h', default: 'localhost' },
  input: { short: 'i', multi: true },
  r:     { flag: true },
  db:    { default: 'test' },
  out:   { short: 'o', type: options.type.file.open.write() }
});

console.log(result);

Running the above code with

node example.js --user=joe -a --host www.example.com output.txt -i file1.txt --input file2.txt -o out.txt

would produce

{ opt: 
   { user: 'joe',
     all: true,
     host: 'www.example.com',
     db: 'test',
     input: [ 'file1.txt', 'file2.txt' ],
     out: { name: 'out.txt', fd: 9 } },
  args: [ 'output.txt' ] }

Reference

options.parse(opts);
options.parse(opts, argv);
options.parse(opts, error);
options.parse(opts, argv, error);

The first argument to .parse(...) is always the options object defining what to look for. Every key in the options object denotes the name of the option. The value is another object that can contain one of the following options

{ 
  short: [char],            // short alias for this option (must have length = 1),
  
  required: [bool],         // if set to true, this option is required (default: false)
  
  flag: [bool],             // if true, this option takes no values. If false, this 
                            // option expects a value (ie. --host www.example.com)
                            // (default: false)
  
  default: [string],        // default value of option if it is not present. 
                            // does not make sense to use with flag or required
  
  multi: [bool]             // If true, this option can be specified multiple times
                            // and the resulting value will be an array (default: false)
                            // If false, later options take precedence over earlier options
  
  help: [string],           // help text used when outputting help screen (see below),
  
  showHelp: [bool|object],  // automatically show help screen when this option is present 
                            // (implies flag: true if unset)
  
  varName: [string],        // name to use for value placeholder in help screen (see below)

  type: [Function}          // Type validation. See more details below.
}

You can pass an argv array as the second argument (optional). It should not contain the interpreter (node) and script name. This value defaults to process.argv.slice(2).

It is also possible to pass a string that will then be converted to the corresponding argv array before it will be processed. This can be useful for reading command line parameters from a file.

The error parameter is a callback to use when errors are encountered. If nothing is provided, the default error handling will throw an exception.

Help screen

options-parser can automatically generate a suitable help screen for displaying the available options and help text for the user.

options.help(opts, options);

opts is the same object you would pass to options.parse(...). options is an optional object literal with the following structure

{
  columns: [number],          // format text to fit the specified number of columns
                              // if columns is not specified, it defaults to the current
                              // screen with
  
  output: [function],         // use [function] instead of util.puts for output 
                              // (must have same signature)
  
  skipEmpty: [bool],          // leave out options that do not have a help text
  
  paddingLeft: [number],      // left padding of output (default: 2)
  
  separator: [string],        // separator string to use between option names 
                              // and help text (default: '   ')
  
  banner: [string],           // specify a banner to display as the first line of help output
  
  // the following are only available when passed to showHelp property of an opts object
  
  noExit: [bool],             // do not exit after displaying help screen invoked from 
                              // a showHelp option (default: false)
                              
  callback: [function]        // funciton to be called after help screen has been displayed
  
}

You can pass a options object literal to the showHelp property in the opts object literal. This will be passed directly to the .help(...) method when the options is encountered.

Example 2

// help-example.js
var options = require('options-parser');
options.parse({
  file: {
    short: 'f',
    varName: 'FILE',
    help: 'file to process'
  },
  q: {
    help: 'do not print any output',
    flag: true
  },
  help: {
    short: 'h',
    help: 'this help screen',
    showHelp: { 
      banner: 'options-parser example: [options]'
    }
  }
});
$ node help-example.js --help
options-parser example: [options]
  --file FILE, -f FILE   file to process
  -q                     do not print any output
  --help, -h             this help screen

Type Validation

To ease validation of argument values you can pass a function to the "type" member of the options configuration object. The function takes two arguments: the value to validate and a replacement function that takes one argument. If the validation succeeds return true, otherwise return an error message string.

The replacement function can be used to replace the value being validated with a new value. Say you want to see if the value is a valid date. You can then use the replacement function to replace the string value of the input with a Date object representing the parsed date.

There are a few built-in type validation functions:

var options = require('options-parser');

// require parameter value to be an integer
options.type.int(errorMessage);

// require parameter value to be a valid file and open it for reading and writing
options.type.file.open(errorMessage, options);

// require parameter value to be a valid file and open it for reading
options.type.file.open.read(errorMessage, options);

// require parameter value to be a valid file and open it for writing
options.type.file.open.write(errorMessage, options);

The errorMessage string is optional. If no error message is passed a default error message will be used.

The options object can have two fields: flags and mode which reflect the flags and mode parameters of the [fs.open api] (http://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback) function.

The file.open.xxx functions all replace the value with an object with two fields: name and fd. name is the name of the file as specified by the user and fd is the file descriptor of the opened file. See the first example near the top of the page