Open ashmaroli opened 1 year ago
I like this! It'd be really cool if we could mark the options as required inline instead of needing to do that in the validate
func.
It'd be really cool if we could mark the options as required inline instead of needing to do that in the
validate
func.
OptionParser
itself doesn't have a builtin mechanism to enforce required options.
However, if given a Class for Type Coercion, it can throw error when passed an empty string. For example,
cmd.option "--export-file PATH", "Path to export file."
will not fail when --export-file
is omitted or passed an empty string. But,
cmd.option "--export-file PATH", String, "Path to export file."
will fail when --export-file
is passed an empty string. Omission is legal. (as of bundled version in Ruby 2.7.x).
Update: Just found out that we can define custom type coercion via OptionParser#accept
🎉
Jotting this down for feedback and for future reference.