Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

cliopts

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

cliopts

Simple Typescript CLI options parser.

unpublished
latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

cliopts

Simple Typescript CLI options parser.

Usage

Parse process arguments into options.

const options = parseArgs(process.argv.slice(2), {
  true: { type: true }, // No value
  false: { type: false }, // No value
  string: { type: String }, // String value
  number: { type: Number, alias: 'b' }, // Number value
});

Read the options.

options.get('true'); // true | undefined
options.get('false'); // false | undefined;
options.getAll('string'); // string[]
options.getRequired('number'); // number (or throw)
options.names; // array of all parsed named options
options.positional; // All un-named (positional) options

The get method returns the last value if the argument is repeated.

const options = parseArgs(['--foo=1', '--foo=2'], {
  foo: { type: Number },
});

options.get('foo'); // 2

Errors are thrown...

  • When unknown named options are encountered.
  • When an option requires a value but a value is not given.
  • When an option does not accept a value but a value is given.
// Throws because --foo is not an option.
const options = parseArgs(['--foo'], {
  bar: { type: String },
  baz: { type: true },
});

// Throws because --bar requires a value.
const options = parseArgs(['--bar'], {
  bar: { type: String },
  baz: { type: true },
});

// Throws because --baz does not accept a value.
const options = parseArgs(['--baz=1'], {
  bar: { type: String },
  baz: { type: true },
});

All arguments following -- are treated as positional (unnamed) options, even if they start with hyphens.

const options = parseArgs(['--foo', '--', '--bar'], {
  foo: { type: true },
  bar: { type: true }
});

options.has('bar'); // false
options.names; // ['foo']
options.positional; // ['--bar']

Keywords

cli

FAQs

Package last updated on 21 Aug 2022

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