Relational rules between options: when something must be present, when things can coexist, when they cannot.
Conditional required
:required_if
Required when another option holds a specific value:
option :format, type: :string, choices: ["json", "table"]
option :output, type: :string, required_if: [format: "json"]error: --output is required when --format is 'json'Multiple conditions: any match triggers the requirement.
option :region, type: :string, required_if: [env: "prod", env: "staging"]Use :required_if_all when every condition must hold instead of any:
option :confirm, type: :boolean, required_if_all: [env: "prod", force: true]
# error: --confirm is required when --env is 'prod' and --force is true:required_unless
Required unless any of the named options is present. Accepts an atom or a list:
option :config, type: :string, required_unless: :inline
option :config, type: :string, required_unless: [:inline, :stdin]error: --config is required unless --inline, --stdin is providedUse :required_unless_all to require the option unless all of the named
options are present:
option :out, type: :string, required_unless_all: [:a, :b]
# error: --out is required unless --a, --b are all providedPer-option relations
:conflicts_with
Declares that two options cannot be set together:
option :json, type: :boolean, conflicts_with: :yaml
option :json, type: :boolean, conflicts_with: [:yaml, :toml]error: --json cannot be used with --yaml:requires
The inverse: setting one option implies another must also be set:
option :user, type: :string, requires: :password
option :deploy, type: :boolean, requires: [:env, :region]error: --deploy requires --envGroups
Group a set of options under a named constraint.
Mutually exclusive
At most one of the group's options can be set:
group :format, mutually_exclusive: true do
option :json, type: :boolean
option :csv, type: :boolean
option :yaml, type: :boolean
enderror: options --json, --yaml are mutually exclusive (group: format)Co-occurring
All or none of the group's options must be set:
group :auth, co_occurring: true do
option :username, type: :string
option :password, type: :string
enderror: options --username, --password must be used together (group: auth)Required (one-of)
At least one of the group's options must be set:
group :source, required: true do
option :file, type: :string
option :stdin, type: :boolean
enderror: one of --file, --stdin is required (group: source)Constraints combine: mutually_exclusive: true, required: true means exactly one
member must be set.
Picking the right tool
- One option conditionally required based on another's value:
:required_if. - One option required unless another is present:
:required_unless. - Two or three specific options that must or must not coexist:
:conflicts_with/:requireson a single option. - A larger set of options with one constraint (format flags, auth flags):
groupwithmutually_exclusiveorco_occurring. - Anything with its own logic: a cross-param
validatefunction.
See Validation for the latter.