tinyargs

  • Version 0.1.4
  • Published
  • 17.2 kB
  • No dependencies
  • MIT license

Install

npm i tinyargs
yarn add tinyargs
pnpm add tinyargs

Overview

A tiny and flexible command-line argument parser for Node.js and Deno.

Index

Functions

function parse

parse: <TMeta>(args: string[], options: Option<TMeta>[]) => Parsed;
  • Parse command line arguments with give option config

    Not that if you're using process.argv, you should always omit the first two elements, i.e. pass process.argv.slice(2) to this function

Interfaces

interface Option

interface Option<TMeta> {}

    property flags

    flags?: string[];
    • By default we only capture flags that match the option name But you can use flags to capture flags that match any of the specified values

    property meta

    meta?: TMeta;
    • Use this to store information about the option Useful for building help message

    property multiple

    multiple?: boolean | 'include-flags';
    • Allow multiple flags or positional arguments

      For flags, this means that the flag can be repeated.

      Example 1

      $ node cli.js --foo --foo --foo
      {foo: [true, true, true]}
      $ node cli.js --foo 1 --foo 2 --foo 3
      {foo: ['1', '2', '3']}

      For positional arguments, you can provide multiple values. It only captures non-flag value (not starting with a dash -) by default but you can set it to include-flags to capture all values

      Example 2

      $ node cli.js a b c
      {some_optiona: ['a', 'b', 'c']}

    property name

    name: string;
    • The flag or positional argument's name

      For example, name: 'file' will capture the value of flag --file

    property optionalValue

    optionalValue?: boolean;
    • Allow optional value

      For a non-boolean flag, this means that the flag value can be omitted

      Example 1

      node cli.js --config
      # `config` will be `true` instead of throwing an error

      For a positional argument, this means it could be undefined in the returned object

    property positional

    positional?: boolean;
    • Mark this optional as a positional argument

    property stop

    stop?: boolean;
    • Stop parsing remaining arguments after current option

      The remaining arguments will be added to the _ key

    property type

    type: OptionSimpleType;
    • Option type

      We use the function to coerce the value

    property when

    when?: (parsed: Parsed) => boolean;
    • Only add this option if this function returns true

      Already parsed options will be passed to the function

    Type Aliases

    type Parsed

    type Parsed = {
    _: string[];
    [key: string]: any;
    };

      Package Files (1)

      Dependencies (0)

      No dependencies.

      Dev Dependencies (8)

      Peer Dependencies (0)

      No peer dependencies.

      Badge

      To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

      You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/tinyargs.

      • Markdown
        [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/tinyargs)
      • HTML
        <a href="https://www.jsdocs.io/package/tinyargs"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>