@3xpo/argparser

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

Generated using TypeDoc