1.1.2 โ€ข Published 5 months ago

@3xpo/argparser v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

ArgParser ๐Ÿ”

๐Ÿงช Tests ๐Ÿ“ Documentation ๐Ÿ“ฆ NPM ๐Ÿ“ฆ Bundle Size ๐Ÿ“ Source Size Parse NodeJS CLI arguments with ease.

๐Ÿ“ฆ Table of Contents

๐Ÿš€ Setup

pnpm i @3xpo/argparser

๐Ÿ› ๏ธ Usage

import ArgParser from '@3xpo/argparser';

const argParser = new ArgParser()
  .defineArgument({
    type: 'string',
    name: 'arg1',
    aliases: ['a', 'b'],
    default: 'default value',
    description: 'This is a description of the argument',
  })
  .defineArgument({
    type: 'boolean',
    name: 'arg2',
    aliases: ['c'],
    default: 'default value',
    description: 'This is a description of the argument',
  });

// generic is inferred, and can be overwritten to manually specify arg types
const args = argParser.parse(['--arg1', 'value1', '-c', 'hi']); // => { arg1: 'value1', arg2: true, _: ['hi'] }
const args2 = argParser.parse(['--arg1', 'value1', '-c', 'false', 'hi']); // => { arg1: 'value1', arg2: false, _: ['hi'] }

Or, if you prefer non-inferred types:

import ArgParser from '@3xpo/argparser';

const argParser = new ArgParser();
argParser.defineArgument({
  type: 'string',
  name: 'arg1',
  aliases: ['a', 'b'],
  default: 'default value',
  description: 'This is a description of the argument',
});
argParser.defineArgument({
  type: 'boolean',
  name: 'arg2',
  aliases: ['c'],
  default: 'default value',
  description: 'This is a description of the argument',
});

// generic is optional, however necessary for typesafety
const args = argParser.parse<{
  arg1: string;
  arg2: boolean;
}>(['--arg1', 'value1', '-c', 'hi']); // => { arg1: 'value1', arg2: true, _: ['hi'] }
const args2 = argParser.parse<{
  arg1: string;
  arg2: boolean;
}>(['--arg1', 'value1', '-c', 'false', 'hi']); // => { arg1: 'value1', arg2: false, _: ['hi'] }

For more detailed examples, see the ๐Ÿงช Tests. For technical reference, see the ๐Ÿ“ Documentation.

๐Ÿ“œ License

This project is licensed under the ๐Ÿ“„ MIT License

๐Ÿ”— See Also

1.1.1

5 months ago

1.1.0

5 months ago

1.1.2

5 months ago

1.0.3

8 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago