tj / commander

The complete solution for Ruby command-line executables
http://visionmedia.github.com/commander
MIT License
1.09k stars 64 forks source link

Question : ctrl-c and listener #64

Closed cmoulliard closed 10 years ago

cmoulliard commented 10 years ago

As Commander is able to intercept CTRL-C event (shutdown)

trap('INT') { abort program(:int_message) } if program(:int_message)

I would like to know if there is a event/listener available from Commander that we can use when such event occurs to call a method to kill a thread by example

The thread has been created when we have have call the function the first time the command has been received

c.action do |args, options|
Program::MyProject.process(options) # where we will create a Thread
end
ggilder commented 10 years ago

You can use program :int_block to provide a block that will get called on interrupt. Something like:

program :int_block do
  # kill threads etc
  # make sure to exit or abort, otherwise your program keeps running
  exit 1
end