πŸ“¦ ljharb / pargs

a useful wrapper for `util.parseArgs`

β˜… 6 stars β‘‚ 0 forks πŸ‘ 6 watching
argument-parserparseargsutil
πŸ“₯ Clone https://github.com/ljharb/pargs.git
HTTPS git clone https://github.com/ljharb/pargs.git
SSH git clone git@github.com:ljharb/pargs.git
CLI gh repo clone ljharb/pargs
Jordan Harband Jordan Harband [readme] replace runkit CI badge with shields.io check-runs badge ba954d0 7 days ago πŸ“ History
πŸ“‚ main View all commits β†’
πŸ“ .github
πŸ“ test
πŸ“ types
πŸ“„ .attw.json
πŸ“„ .gitignore
πŸ“„ .npmrc
πŸ“„ .nycrc
πŸ“„ CHANGELOG.md
πŸ“„ index.d.mts
πŸ“„ index.mjs
πŸ“„ package.json
πŸ“„ README.md
πŸ“„ tsconfig.json
πŸ“„ README.md

pargs Version Badge

github actions coverage License Downloads

npm badge

A wrapper for node’s built-in util.parseArgs with helpful features added.

Usage

#!/usr/bin/env node

import pargs from 'pargs';

const {
    help,
    positionals,
    values,
    errors, // a mutable string array; push to it and pargs will include your error messages.
    name, // if subcommands are used
    tokens,
} = await pargs(import.meta.filename, options);

// do extra validation here

await help(); // to handle `--help` and print the help text if needed, or to print errors and exit

Help

Help text is automatically read from a help.txt file adjacent to import.meta.filename.

await an invocation of the help function returned from the pargs call to handle --help and print the help text if needed, or to print errors and exit.

Options

See the node.js parseArgs documentation for some context.

  • strict: can not be set to false - strictness all the way.
  • allowNegative: can not be set to false.
  • args: can not provide; pargs always uses process.cwd() - this may be added in the future, though.
  • options.type: in addition to 'boolean' and 'string', 'enum': when provided, a choices string array is also required.
  • allowPositionals: in addition to a boolean, or an integer representing the maximum number of allowed positional arguments.
  • minPositionals: an integer representing the minimum required number of positional arguments.
  • subcommands: if provided, must be an object. Keys are the subcommand names (eg, in npm ls, ls is the subcommand), and values are the configuration options for each subcommand - as if they were a top-level invocation.

Install

` %%CODEBLOCK1%% ## License MIT ## Thanks Thanks to [@ibakaidov] for donating the pargs` package name!