davetron5000 / optparse-plus

Start your command line scripts off right in Ruby
http://davetron5000.github.com/optparse-plus
Apache License 2.0
521 stars 54 forks source link

Raise exception option for sh! #54

Closed joeworkman closed 11 years ago

joeworkman commented 11 years ago

I am really loving Methadone. Thank you!

I like the sh! method, however, I wish that there was the ability to raise an exception as well as exiting. Maybe this could be a new method?

If I am running sh from within a class, I feel its bad practice to exit the application form there. Let me know if I am wrong here though.

This is the work around that I can come up with for my classes and it seems to work awesome.

begin
  sh "opens '#{notifyurl}'"
rescue Exception => e 
  raise e.message 
end
davetron5000 commented 11 years ago

That code just re-raises the exception you've caught, so it doesn't really do anything additional.

sh! actually does raise an exception - that's why it's there. It raises a Methadone::FailedCommandError which, if you do not catch it, will ultimately cause your app to exit. In a methadone-powered app, any unhandled exception is ultimately caught, and the app exits with the message give to the exception.

The idea of sh! is that you would use it for commands that absolutely have to succeed or your program must die. You can customize the error message on a failure via

sh!("ls -l /tmp", :on_fail => "Couldn't ls /tmp?  Uh-oh!")

Now, all that being said, maybe you are doing something I hadn't considered? If this explanation didn't help, post some more code and maybe I can figure out what we need to do…

davetron5000 commented 11 years ago

Assuming my answer helped, so closing. Re-open with more info if I'm missing something in my answer or if you find another problem.