2.0.11 • Published 4 months ago

@jakubmazanec/args v2.0.11

Weekly downloads
-
License
LGPL-3.0-only
Repository
github
Last release
4 months ago

@jakubmazanec/args

TypeScript-first library for parsing command line arguments.

Installation

npm install @jakubmazanec/args

⚠️ This is an ESM package! It cannot be required from a CommonJS module.

Prerequisites

  • Node.js 20 or later
  • TypeScript 5 or later

Usage

Main purpose of this library is to parse command line arguments. Such command line can look like this:

┌─ 1 ─┐ ┌─ 2 ──┐ ┌─── 3 ───┐ ┌─── 4 ───┐ ┌─ 5 ─┐ ┌ 6 ┐ ┌─── 7 ───┐    ┌─────── 8 ──────┐
binname do stuff --key value --key=value -key -K -aBCd foo bar baz -- --key value bar -B
└───────────────────────────────────────── 9 ──────────────────────────────────────────┘
  1. Binary name. This is never passed to the parser.
  2. Command name.
  3. Option with a value. Option name can be in camel case or kebab case.
  4. Another way to specify an option with a value.
  5. Boolean options don't have to specify value to be true. Options can also be defined using a short option form.
  6. Short options group; usually used for boolean options.
  7. Parameters.
  8. Rest arguments appear after the --; they are not parsed, but still included in the result.
  9. The whole command line.

The parser is strict and returns fully typed result. You can use it by calling parseArguments function with the list of arguments and parser configuration:

import {parseArguments} from '@jakubmazanec/args';

let {command, parameters, options} = parseArguments(process.argv.slice(2), {
  commands: ['run', 'build'],
  parameters: [{
    description: 'File name',
    type: 'string',
    required: true,
  }],
  options: {
    help: {
      type: 'boolean'
    }
  }
});

console.log(command); // type of `command` is `'run' | 'build' | undefined`
console.log(parameters); // type of `parameters` is `[string | undefined]`
console.log(options); // type of `options` is `{help: boolean | undefined}`

See ParserConfig for more information about how to configure the parser.

Documentation

See API reference for auto-generated documentation.

Contributing

If you want to contribute, see CONTRIBUTING for details.

License

This package is licensed under the GNU Lesser General Public License v3. See LICENSE for details.

2.0.4-next.070f0f6

12 months ago

2.0.11

9 months ago

2.0.10

9 months ago

2.0.7-next.756b5671

10 months ago

2.0.5-next.ebf1317

11 months ago

2.0.8-next.e20bd97a

10 months ago

2.0.4-next.86bf251

12 months ago

2.0.8-next.e176b925

10 months ago

2.0.5-next.0f7252e

11 months ago

2.0.6-next.e10ee8f6

11 months ago

2.0.4-next.5b99361

12 months ago

2.0.4-next.c443585

12 months ago

2.0.8-next.3517a43a

10 months ago

2.0.7-next.553a704e

10 months ago

2.0.8-next.50ba7658

10 months ago

2.0.8-next.335706bb

10 months ago

2.0.5

11 months ago

2.0.4

12 months ago

2.0.7

10 months ago

2.0.6

11 months ago

2.0.9

9 months ago

2.0.8

10 months ago

2.0.4-next.53120c5

12 months ago

2.0.9-next.831db908

10 months ago

2.0.5-next.80f405d

11 months ago

2.0.5-next.b522685

11 months ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1-6cee770

2 years ago

1.0.1-c2efc51

2 years ago

1.0.1-dee6883

2 years ago

1.0.1-8a0a4d5

2 years ago

1.0.2-a1d4551

2 years ago

1.0.1-2f12db0

2 years ago

1.0.1-bc209e5

2 years ago

1.0.1-7dbb232

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.2-2487994

2 years ago

1.0.2-b8faa49

2 years ago

1.0.1-2d1145f

2 years ago

1.0.1-5f6d70c

2 years ago

1.0.1-c5ebb6f

2 years ago

1.0.1-593aae1

2 years ago

1.0.1-265acad

2 years ago

1.0.1-11dd4a3

2 years ago

1.0.1-9029e49

2 years ago

1.0.1-a500263

2 years ago

1.0.0

2 years ago

1.0.0-fea335c

2 years ago

1.0.0-de51f16

2 years ago