JSPM

io-ts-docopt

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 14186
  • Score
    100M100P100Q143707F
  • License ISC

Decode docopt output with io-ts

Package Exports

  • io-ts-docopt

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (io-ts-docopt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

io-ts-docopt

License NPM Package Build status Code Coverage

Decode docopt output with io-ts

Docopt is a fantastic argument-parsing tool, but suffers from a few drawbacks:

  1. The returned object has awkward keys like <name> that cannot be accessed with the dot accessor
  2. Arguments (except flags) are always parsed as strings

This project combines docopt with io-ts to overcome both inconveniences, by first parsing arguments with docopt, then decoding and then encoding the parsed value through the specified io-ts codec. This sequence provides the opportunity to remap gnarly keys into more convenient identifiers, as well as decode values from one type into another (for example, with the NumberFromString codec).

Install

npm install io-ts-docopt

Use

import { decodeDocopt, withEncode } from 'io-ts-docopt'

const docstring = `
Usage:
    foo --clean <one> <two> <many>...
`

const codec = withEncode(
    iots.type({
        '--clean': iots.boolean,
        '<one>': iots.string,
        '<two>': iots.string,
        '<many>': iots.array(iots.string)
    }),
    (a) => ({
        clean: a['--clean'],
        one: a['<one>'],
        two: a['<two>'],
        many: a['<many>']
    })
)

decodeDocopt(
    codec,
    docstring,
    {argv: `--clean red blue green purple yellow`.split(/\s+/)}
)
//=> {
//       clean: true,
//       one: "red",
//       two: "blue",
//       many: ["green", "purple", "yellow"]
//  }

Documentation

See generated documentation.

Acknowledgments