@3xpo/argparser
v1.1.2
Published
parse node args
Downloads
10
Maintainers
Readme
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