Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

argcat

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

argcat

The simplest CLI arguments parser.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

argcat

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 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: [] }

Type coercion

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

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

const argsShape = d.object({
  foo: d.number()
});

const options = argsShape.parse(
  // 😉 Use process.argv.slice(2) instead of an array here 
  parseArgs(['--age', '42']),
  { coerce: true }
);
// ⮕  { age: 42 }

Keywords

FAQs

Package last updated on 26 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc