@3xpo/argparser
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i @3xpo/argparser

Weekly Downloads

12

Version

1.1.2

License

MIT

Unpacked Size

20.1 kB

Total Files

14

Last publish

Collaborators

  • exponentialworkload