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

gnu-option

v1.0.1

Published

A simple parser for GNU style command line options

Downloads

5

Readme

gnu-option

Build Status Coverage Status NPM MIT

A simple parser for GNU style command line options

  • Supports GNU style long options that start with two dashes. (eg. --file="filename" or --file filename)
  • Supports POSIX style options that start with one dash. (eg. -cf filename or -c -f filename)
const gnuopt = require('gnu-option');

const tar_optmap = {
  'c': '&create', // -c is alias of --create
    // leading ampersand(&) means an alias
  'create': 'switch', // --create is switch
  'f': '&file', // -f is alias of --file
  'file': 'file', // file is string typed value
};

gnuopt.parse(['-cf', 'npmdir.tar', 'npmdir'], tar_optmap);
// { create: 1, file: 'npmdir.tar',  $: ['npmdir'] }

const sed_optmap = {
  'e': '&expression', // -e is alias of --expression
  'expression': '*string', // --expression is string
    // leading asterisk(*) means that such a option can appear multiple times
  'r': '&regex-extended', // -r is alias of --regex-extended
  'regex-extended': 'switch', // --regex-extended is switch
}

gnuopt.parse(['-re', 's/a/b/', '-e', 's/b/c/'], sed_optmap);
// { 'regex-extended': 1, 'expression': [ 's/a/b/', 's/b/c/' ], $: [] }

gnuopt.parse(sed_optmap);
// When only optmap is specified, process.argv.splice(2) is used

optmap

optmap (option map) is an important argument of parse method. Represents definition map including type and alias informations.

For example, --help and -c options like bash can be defined as follow:

var optmap = {
  'h': '&help',
  'help': 'switch'
  'c': '&command',
  'command', '~string'
}

Alias definition

To define aliaes (eg. --h for --help), use a combination between leading amphersand and target option name.

On the following example, able refers baker, baker refers charile, charlie refers dog, and ...

var optmap = {
  able: '&baker',
  baker: '&charile',
  charile: '&dog',
  //...
}

NOTE: If reference is looped, CircularReferenceError will be thrown to prevent infinite loop.

Built-in Types

Avaiable built-in types are the followings:

  • string: accepts any string value without type specific verification.
  • number: accepts numerical value (parsed by parseFloat)
  • integer: accepts integral value (parsed by parseInt)
  • switch: switch typed option never have its value. Similar to -f option of rm command.

Leading tilde before type name

Leading tilde ~ means that such a option subsequently collect the all thing as its value. Similar to -c option of bash.

gnuopt.parse(['-c', 'curl', '-o', 'foo.bar', 'http://foo.bar/'], {
  c: '~string'
});
// { c: [ 'curl', '-o', 'foo.bar', 'http://foo.bar/' ] }

Leading asterisk before type name

Leading asterisk * means that such a option can be present multiple times. By default, throws InvalidRepetationError when same named option are present.

gnuopt.parse(['-d', 'dup1', '-d', 'dup2'], { d: '*string' });
// { d: ['dup1', 'dup2'] }

NOTE: Because switch does not own no value, *switch is commonly strange. Therefore *switch is treated as special: its value will represent number of its occurences.

gnuopt.parse('-ccccc', { c: '*switch' });
// { c: 5 }

Custom Type

By specifying parser-function instead of type name, can declare the custom typed option.

function parseInterval(val) {
  var m =/^([0-9]+)([mh])$/i.exec(val);
  if (!m) // Thrown error is set to InvalidValueError.innerError
    throw new Error("interval type is expected");
  return {
    value: parseInt(m[1]);
    unit: m[2],
  }
}
var optmap = {
  wait: parseInterval,
}

Errors

The following errors will be thrown when verification is failed. Each errors has some properties to determine which options was related with it.

InvalidValueError

Thrown when value is not valid type.

  • name: 'InvalidValueError'
  • option: related option name excluding leading dashes
  • value: actual invalid value of option
  • type: expected type of option
  • innerError: error thrown on custom parser function.

InvalidRepetationError

Thrown when same named options will be specified multiple times unless asterisk * is specified.

  • name: 'InvalidRepetationError'
  • option: related option name excluding leading dashes

UndefinedTypeError

Thrown when undefined type is specified on optmap

  • name: 'UndefinedTypeError'
  • option: related option name excluding leading dashes
  • type: undefined type name of option

CircularReferenceError

Thrown when ampersand-reference is looped on optmap

  • name: 'CircularReferenceError'
  • option: related option name excluding leading dashes
  • route: array of referred option aliases

Tips

If you want to first argument treat as a command like git,

use process.argv[3] to determine command, and use process.argv.splice(3) for parse` method.

const gnuopt = require('gnu-option')
var optmap;
switch (process.argv[3]) {
  case 'foo':
    optmap = { /* optmap for foo command */ }
    break;
  case 'bar':
    optmap = { /* optmap for bar command */ }
    break;
}
gnuopt.parse(process.argv.splice(3), optmap);

License

Distributed under the MIT license

Copyright (C) 2016 Retorillo