Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options validation #238

Open
timfjord opened this issue Jul 16, 2012 · 4 comments
Open

Options validation #238

timfjord opened this issue Jul 16, 2012 · 4 comments

Comments

@timfjord
Copy link

Maybe make sense add option validations, so we can validate options and provide error messages if validation not passed

We can implement some predefined validators but also allow use add custom validation methods

Smth like

class Test < Thor
  desc "example FILE", "an example task"
  method_option :date, :aliases => "-d", :desc => "Specify date", :validators => { :date => 'Date shold be valid' }
  method_option :email, :aliases => "-e", :desc => "Email", :validators => { :email }
  def example(file)
    # some code
  end

private

  def date(value)
    # return true or false
  end
end

If you find such functionality useful i can help with implementation

Also we can think about syntax to make it flexible

@jxpx777
Copy link

jxpx777 commented Dec 26, 2012

I was looking for something like this myself. If :validation option took an Array, you could specify a simple list of valid values. With a Proc, some more advanced logic. With a class, it would specify a Ruby object tasked with the validation. Maybe a Thor::Validation module to formalizes the validation API for classes?

@eventualbuddha
Copy link
Contributor

Is there an advantage to this as opposed to doing it yourself in the actual task?

@timfjord
Copy link
Author

Just thinking how to make code DRY

@askreet
Copy link

askreet commented Oct 28, 2013

I think this is a great idea, and a symbol that references a local-scope method seems like a nice simple approach. I may tackle this in the near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants