Abstract getopt handling for GNU vs BSD

I'm hoping this will resolve #715, although I'm not on OSX so I can't easily test it.  As an added bonus, the code is a bit less repetitive and this new function could be reused in future code. I also fixed a typo in the trap function; it was passing `-n type` to GNU getopt.