clinical
v0.0.2
Published
A minimum-viable arguments parser in ~90 LOC with zero dependencies
Readme
clinical

A minimum-viable arguments parser in ~90 LOC with zero dependencies
Features
- Casts values to the appropriate JavaScript primitive type
- Converts option keys to camelCase
- Throws on duplicated options
- Stops parsing options after
-- - Prints the supplied version on
--versionor-v - Prints the supplied help message on
--helpor-h
Example
$ npm install --save clinical#!/usr/bin/env node
import clinical from 'clinical'
try {
const result = clinical('1.0.0', 'my help message')
console.log(result)
} catch (error) {
console.error(error.message)
process.exit(1)
}$ my-cli --foo --bar 42 -x=y -- baz null
{
options: { foo: true, bar: 42, x: 'y' },
positionals: [ 'baz', null ]
}$ my-cli --version
1.0.0$ my-cli --help
my help messageAPI
import clinical from 'clinical'const result = clinical(version, helpMessage [, args = process.argv.slice(2)])
version(string) – Required. Writes this string tostdouton encountering the--versionor-vflag, then exits theprocess.helpMessage(string) – Required. Writes this string tostdouton encountering the--helpor-hflag, then exits theprocess.args(Array<string>) – Optional. The arguments to be parsed. Defaults toprocess.argv.slice(2).
The returned result object has the following keys:
positionals(Array<boolean | null | number | string>)options({ [key: string]: boolean | null | number | string })
Installation
$ npm install --save clinical