1.1.2 โข Published 5 months ago
@3xpo/argparser v1.1.2
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