diff options
author | Victor Gama <hey@vito.io> | 2023-02-10 16:26:00 -0300 |
---|---|---|
committer | Victor Gama <hey@vito.io> | 2023-02-15 11:43:06 -0300 |
commit | 25cae16e2c3ca2fbe8c1c75923dae9b14cc38b72 (patch) | |
tree | 5f79f933524fbcb0af283b4ffa8f90e6e5827b82 | |
parent | a23fa41a5674485600365985bea2a905e5e087df (diff) | |
download | slop-25cae16e2c3ca2fbe8c1c75923dae9b14cc38b72.tar.gz |
Update README
-rw-r--r-- | README.md | 31 |
1 files changed, 30 insertions, 1 deletions
@@ -204,6 +204,8 @@ Slop will raise errors for the following: * An option used without an argument when it expects one: `Slop::MissingArgument` * An option used that Slop doesn't know about: `Slop::UnknownOption` * An option marked as `required` when not provided: `Slop::MissingRequiredOption` +* An option marked as `validate_types`, with an argument that does not match its +type (i.e. `bla` for `integer`): `Slop::InvalidOptionValue` These errors inherit from `Slop::Error`, so you can rescue them all. Alternatively you can suppress these errors with the `suppress_errors` config @@ -222,6 +224,33 @@ opts = Slop.parse do end ``` +Validating Types +---------------- + +By default, Slop does not validate whether an argument is a valid value for a +given option; instead, if the option has a default value, it will be used over +the invalid argument provided. +In order to have types (such as `integer` and `float`) validate and indicate +that the provided value is invalid, an extra option can be either provided to +the argument itself, or its option set: + +```ruby +opts = Slop::Options.new +opts.int "-p", "--port", "a port", default: 80, validate_types: true + +parser = Slop::Parser.new(opts) +result = parser.parse(["--port", "bla"]) +# invalid value for -p, --port (Slop::InvalidOptionValue) + +# Or to the option set... +opts = Slop::Options.new(validate_types: true) +opts.int "-p", "--port", "a port", default: 80 + +parser = Slop::Parser.new(opts) +result = parser.parse(["--port", "bla"]) +# invalid value for -p, --port (Slop::InvalidOptionValue) +``` + Printing help ------------- @@ -279,4 +308,4 @@ end Commands -------- -Slop not longer has built in support for git-style subcommands. +Slop no longer has built in support for git-style subcommands. |