113
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.
Using “or” to define a function that does “xor”… Did that guy never hear about formal logic? That’s, like, first or second semester stuff…
sigh
Could have used
oneOf
orexactlyOne
, butor
is definitely a bad choice.It’s an understandable interpretation for the lexical use of or which can imply exclusive disjunction.
In Rust the result type has the method
.or()
which returns eitherOk(A)
orOk(B)
(but not both), and I don’t see clambering to change it toxor
, because the exclusive nature is implicit both linguistically and in the type state.The result type in rust does not return a true/false but a type. More importantly though, it doesn’t return err if both values are set but simply returns the first value:
So… It’s not only not mapping your input to truth values, it also behaves more like I’d expect an “or” to behave, which is not “xor” or, if there’s more than two inputs, “exactly one”, but succeeding if any input is set.
…Which is basically how the OP’s
or
function also works, it takes severalOption<T>
s and returns the first valid one (and only that one), it doesn’t operate on boolean logic types — it’s a valid lexical use ofor
.Absolutely not.
Emphasis mine.
It takes the input and fails if there is more than one valid one, which decidedly isn’t what’s an “or” in comp sci.