0.2.1 ā€¢ Published 7 months ago

@hypernym/args v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@hypernym/args

A fast and ultra lightweight CLI argument parser.

Repository | Package | Releases | Discussions

npm i @hypernym/args

Features

  • TypeScript friendly
  • Fully tree-shakeable
  • No dependencies

Parser

Arguments

Unprefixed inputs are stored in an array.

$ arg

# => { _: ['arg'] }
$ arg-a arg-b arg-c

# => { _: ['arg-a', 'arg-b', 'arg-c'] }

Flags

Inputs with -- prefix are parsed as flags.

By default, standalone flags with no value are defined as true.

$ --flag

# => { _: [], flag: true, }
$ --flag value

# => { _: [], flag: 'value', }

Aliases

Inputs with - prefix are parsed as aliases.

By default, standalone aliases with no value are defined as true.

$ -alias

# => { _: [], alias: true, }
$ -alias value

# => { _: [], alias: 'value', }

Ignores

  • Ignores standalone inputs -- and -
  • Ignores all inputs that include =
$ --flag=value -- arg=value - -alias=value

# => { _: [] }

Usage

$ hello world --foo bar -baz -cli demo --fuz
import { createArgs } from '@hypernym/args'

interface Args {
  foo?: string
  baz?: boolean
  cli?: string
  fuz?: boolean
}

const args = createArgs<Args>()

console.log(args)

/* Output:
{
  _: ['hello', 'world'],
  foo: 'bar',
  baz: true,
  cli: 'demo',
  fuz: true
}
*/

Options

argv

  • Type: string[]
  • Default: process.argv.slice(2)
const args = createArgs({
  argv: process.argv.slice(2),
})

alias

  • Type: object
  • Default: undefined
const args = createArgs({
  alias: {
    config: ['conf', 'c'],
    help: 'h',
  },
})

Community

Feel free to use the official discussions for any additional questions.

License

Developed in šŸ‡­šŸ‡· Croatia

Released under the MIT license.

Ā© Hypernym Studio