Skip to content

Allow messages to be passed as keyword argument #197

@lulalala

Description

@lulalala

Describe the problem

Allow messages to be passed as keyword argument. This is handy for us because we don't want to use a block to configure the ask, instead have prepared a hash to be used as keyword arguments. Hash works better for more dynamic scenarios.

Steps to reproduce the problem

Using this as a starting, working example which would prompt the user if input is invalid:

prompt.ask("What is your email?") do |q|
  q.validate(/\A\w+@\w+\.\w+\Z/)
  q.messages[:valid?] = "Invalid email address"
end

If I move the validate into the keyword argument, it also works fine:

prompt.ask("What is your email?", validate: /\A\w+@\w+\.\w+\Z/ ) do |q|
  q.messages[:valid?] = "Invalid email address"
end

However, once I move messages into the keyword argument:

prompt.ask("What is your email?", validate: /\A\w+@\w+\.\w+\Z/, messages: { valid?: 'foo' } ) 

The custom messages no longer works. It would display the default "Your answer is invalid (must match /\A\w+@\w+.\w+\Z/)"

Actual behaviour

The custom messages can't be passed as keyword arguments.

Expected behaviour

The custom messages can be passed as keyword arguments.

Describe your environment

  • OS version:
  • Ruby version: 3.1
  • TTY::Prompt version: 0.23.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions