JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 15
  • Score
    100M100P100Q49548F
  • License MIT

Native typescript node package to manage commands arguments

Package Exports

  • scanargs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (scanargs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

scanargs

Typescript native node package to manage commands arguments

Notes

  • Since Node.js 10.0.0
  • Typescript compiled for ECMAScript target version ES2017 (aka ES8)

Setup

$ npm install scanargs

Usage

In your command, set arguments as below

$ example --port=8080 --logger=true
$ example -p=8080 -l=true

In your javascript code, require the scanargs package

const { Scanargs } = require('scanargs');

In your typescript code, import the scanargs package

import { Scanargs } from ('scanargs');

Typescript example

Code

import { Scanargs } from 'scanargs';
const scan = new Scanargs(process.argv, [{ name: 'param' }]);
scan.options = [{ name: 'port', default: 3000 }, { name: 'logger', default: false }, { name: 'throwError' }];

console.log(`${JSON.stringify(scan.scan(), null, 2)}`);

Command

$ ts-node example.ts --port="8080" --l=true --throwError=false --arg=azerty

Console

{
  "port": 8080,
  "logger": true,
  "throwError": "false"
}

Note: As 'arg' is not defined in options, it is not returned/recognized as an argument.

Javascript example

Code

const { Scanargs } = require('scanargs');
const scan = new Scanargs(process.argv, [{name: 'param'}]);

console.log(`bin [ ${scan.getNode()} ] file [ ${scan.getNode()} ] param [ ${scan.scan().param} ]`);

Command

$ node example.js --param=azerty

Console

bin [ /usr/bin/node ] file [ /usr/bin/node ] param [ azerty ]

Options

Array of options passed to the constructor.
An option is defined as below

{
  name: string;
  alias: string;
  default: any;
}
  • name is mandatory.
    • It is the name of a command argument you want to retrieve.
    • It defines the string after the double hyphen (--).
      (--name)
  • alias is optional.
    • It is the argument short-name.
    • It defines the string after the hyphen (-).
    • If it is not defined the default alias is defined to 'n', the first letter of name.
      (-n)
  • default is optional.
    • If defined, it gives its type (number, string, boolean) to the passed value.
    • If not defined, the value type is always string