diff options
author | Lee Jarvis <lee@jrvs.uk> | 2023-02-15 11:28:21 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-15 11:28:21 -0800 |
commit | 2410f313984be1dfe73884251e879b008771535d (patch) | |
tree | 8f36d5b4c65685653e0c8ef3c344d5fe75564fd6 /README.md | |
parent | fc4622fc08d7982847ff583ad691ee4ba47f63f8 (diff) | |
parent | becc0305239c2885affb1f57419afac3e5418a88 (diff) | |
download | slop-2410f313984be1dfe73884251e879b008771535d.tar.gz |
Merge pull request #278 from heyvito/feat/validate-types
Add support to optional type validation
Diffstat (limited to 'README.md')
-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. |