Stop writing CLI validation. Parse it right the first time.

洪 民憙 (Hong Minhee) @hongminhee@hackers.pub

This post introduces Optique, a new library created to address the pervasive problem of repetitive and often messy validation code in CLI tools. The author was motivated by the observation that nearly every CLI tool reinvents the wheel with similar validation patterns for dependent options, mutually exclusive options, and environment-specific requirements. Optique leverages parser combinators and TypeScript's type inference to ensure that CLI arguments are parsed directly into valid configurations, eliminating the need for manual validation. By describing the desired CLI configuration with Optique, TypeScript automatically infers the types and constraints, catching potential bugs at compile time. The author shares their experience of deleting large chunks of validation code and simplifying refactoring tasks. Optique aims to provide a more robust and maintainable approach to CLI argument parsing, potentially saving developers from writing the same validation logic repeatedly.

Read more →
19
3
1

If you have a fediverse account, you can quote this article from your own instance. Search https://hackers.pub/ap/articles/0199203f-a6e9-7f90-9462-0af136538c9c on your instance and quote it. (Note that quoting is not supported in Mastodon.)

Every CLI tool has the same validation code hidden somewhere:

  • “option A requires option B”
  • “can't use X and Y together”
  • “this only works in production mode”

I got tired of writing it. So I built something that makes it unnecessary.

https://hackers.pub/@hongminhee/2025/stop-writing-cli-validation-parse-it-right-the-first-time

3
1
0