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

ofi

v1.3.4

Published

Yet another arguments parser

Downloads

44

Readme

ofi

npm npm bundle size License

Yet another argument parser.

A tiny cli flags parser.

Install

npm i ofi

Usage

Import:

// ESM
import { parse } from 'ofi';

// CJS
const { parse } = require('ofi');

Setup options parser:

import { parse } from 'ofi';

parse(process.argv.slice(2), {
     number: ['size'],
     string: ['foo', 'name', 'surname'],
     boolean: ['dice', 'friendly'],
     array: ['list', 'my-numbers'],
     alias: { foo: ['f'] },
     default: { surname: 'obama', list: [] }
});

This would give the following results:

$ node program.js --size=3 --name barack -f baz --no-dice --friendly
{
  _: [],
  size: 3,
  name: 'barack',
  surname: 'obama',
  foo: 'baz',
  dice: false,
  list: [],
  friendly: true
}

$ node program.js --list a b c -N hi there --myNumbers=13,1,2,3 -fas
{
    _: ['hi', 'there'],
    surname: 'obama',
    list: [ 'a', 'b', 'c' ],
    N: true,
    'my-numbers': [ 13, 1, 2, 3 ],
    foo: true,
    a: true,
    s: true
}

API

parse(arguments, options?)

Function that parses given arguments.
Returns an argument object argv which contains all parsed flags.

argv._ includes all arguments that weren't associated with any option.
Any argument after -- (end-of-flags) won't be parsed and will end up in argv._ and argv['--'] if populate-- option is set to true.

arguments

Type: String | Array<String>
String or an array of strings to be parsed.

For example:

import { parse } from 'ofi';

parse(process.argv.slice(2));

options

Type: Object
Options for parsing given arguments.

boolean

Arguments that should be parsed as booleans.
Type: String | Array<String>

{ boolean: ['dice'] }

Booleans prefixed with --no will be treated as negations.\

string

Arguments that should be parsed as strings.
Type: String | Array<String>

{ string: ['name'] }
number

Arguments that should be parsed as numbers.
Type: String | Array<String>

{ number: ['age'] }
array

Arguments that should be parsed as arrays.
Type: String | Array<String>

{ array: ['list'] }
default

Set default values.
Type: Object

{ default: { name: 'joe' } }
alias

Set aliases of options.
Type: Object

{ alias: { foo: ['f'], baz: 'b' } }
populate--

Populate '--' property in Argv with everything after double-dash (--, aka. end-of-flags).

Type: Boolean
Default: false

parseNumber

Should values that look like numbers be parsed into them.

Type: Boolean
Default: true

NOTE: This doesn't apply to flags marked as strings.

shortFlagGroup

Should a group of short options be treated as seperate flags.
Example: -abc -> { a: true, b: true, c: true }

Type: Boolean
Default: true

camelize

Convert results to camel-case.

Type: Boolean
Default: false

coerce

Custom synchronous function for parsing provided argument.

Type: Object
Default: undefined

{ coerce: { len: (arg) => arg + ' cm' } }
unknown

Callback function that runs whenever a parsed flag has not been defined in options.

Type: Function
Default: undefined

{ unknown: function (flag) { console.log('Unknown flag: "%s"', flag); } }

License

MIT 💖