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

argcat

v1.0.2

Published

The simplest CLI arguments parser.

Downloads

7

Readme

The simplest CLI arguments parser.

npm install --save-prod argcat

Usage

import { parseArgs } from 'argcat';

const options = parseArgs(process.argv.slice(2));

Arguments prefixed with a '--' are treated as options:

parseArgs(['--foo']);
// ⮕  { foo: [] }

Options can have values:

parseArgs(['--foo', 'bar']);
// ⮕  { foo: ['bar'] }

parseArgs(['--foo', '--qux', 'bar']);
// ⮕  { foo: [], qux: ['bar'] }

If an option is repeated multiple times then all values are captured in an array:

parseArgs(['--foo', 'bar', '--foo', 'qux']);
// ⮕  { foo: ['bar', 'qux'] }

Arguments that aren't prefixed with minus chars are stored under '' key:

parseArgs(['foo', 'bar']);
// ⮕  { '': ['foo', 'bar'] }

There's a special option '--', after which all arguments are stored under '--' key:

parseArgs(['--', '--foo', 'bar']);
// ⮕  { '--': ['--foo', 'bar'] }

Mark an option as a flag to prevent value capturing:

parseArgs(['--foo', 'bar']);
// ⮕  { foo: ['bar'] }

parseArgs(['--foo', 'bar'], { flags: ['foo'] });
// ⮕  { foo: true, '': ['bar'] }

Flag options have true value instead of an array.

Shorthands

By default, shorthand options are ignored:

parseArgs(['-x']);
// ⮕  {}

To preserve shorthands, use keepShorthands option:

parseArgs(['-x'], { keepShorthands: true });
// ⮕  { x: [] }

Multiple shorthands can be combined:

parseArgs(['-abc'], { keepShorthands: true });
// ⮕  { a: [], b: [], c: [] }

Use shorthand mapping to expand shorthands:

parseArgs(['-x'], { shorthands: { x: 'foo' } });
// ⮕  { foo: [] }

Shorthand options can have values:

parseArgs(['-x', 'bar'], { keepShorthands: true });
// ⮕  { x: ['bar'] }

parseArgs(['-abc', 'bar'], { keepShorthands: true });
// ⮕  { a: [], b: [], c: ['bar'] }

Commands

argcat doesn't have a special treatment for commands syntax, but it can be easily emulated:

const argv = ['push', '--tags'];

const result = parseArgs(argv, { flags: ['tags'] });
// ⮕  { '': ['push'], tags: true }

The first element of '' is a command:

const command = result[''].shift();

if (command === 'push') {
  // Push it to the limit
}

Note that this approach allows user to specify options before the command:

const result = parseArgs(['--tags', 'push'], { flags: ['tags'] });
// ⮕  { '': ['push'], tags: true }

Type coercion

Combine argcat with Doubter to validate parsed arguments and to coerce their types.

import { parseArgs } from 'argcat';
import * as d from 'doubter';

// 1️⃣ Define the shape of a CLI options object
const optionsShape = d
  .object({
    age: d.number()
  })
  .strip();

const options = optionsShape.parse(
  // 2️⃣ Convert process.argv.slice(2) to options
  parseArgs(['--age', '42']),
  
  // 3️⃣ Enable type coercion
  { coerce: true }
);
// ⮕  { age: 42 }