The idea came from Haskell's optparse-applicative, but since TypeScript's API style is so different from Haskell's, I referenced Zod and similar libraries for the API design. For a more detailed intro, please refer to the article I posted on Hackers' Pub!
Optique: CLI Parser Combinator
Optique: CLI Parser Combinator
The author introduces Optique, a new experimental CLI parser library influenced by Haskell's `optparse-applicative` and TypeScript's Zod. Optique uses parser combinators, allowing developers to assemble small parsers like Lego bricks to define the structure of a CLI. Key components include `option()` for defining options, `optional()` and `multiple()` for handling optional or multiple options, and `or()` for mutually exclusive options. The `object()` combinator helps create object structures, while `constant()` is used to assign discriminators for union types. Subcommands can be implemented using `command()` and `argument()` parsers, combined with `or()` for multiple subcommands. While Optique may be more complex for simple CLIs and lacks some features of full CLI frameworks, its approach is well-suited for complex CLI definitions. If you find Optique's approach intriguing, the author encourages readers to explore the introductory documentation and tutorials for a deeper understanding.
hackers.pub · Hackers' Pub
Link author: 洪 民憙 (Hong Minhee)@hongminhee@hackers.pub