Writing GUI applications in Objective-C is easy thanks to Cocoa. It takes care of a lot of mundane things like setting up the standard main function, running the event loop, and handling preferences. If you write a command line application in Objective-C, though, you don’t get very much help. You’ve got to remember to setup an NSAutoreleasePool (assuming you’re not using garbage collection) and parsing command line options is left up to you. This is unfortunate, as handling options “properly” is one of the more important aspects of a command line application. This means handling long options (those with a double dash) and short options (those with a single dash).

Thankfully, you do get some support for parsing command line options from the FreeBSD layer in the form of getopt_long(3). But using getopt_long is a bit of a pain: you’ve got to setup a structure of options, then repeatedly call it, followed by a switch statement. See the example in the man page. Yuck!

In order to simplify all aspects of writing command line applications I’ve written ddcli, a framework for writing command line applications in Objective-C, released under the MIT license, of course. The meat of it is an Objective-C wrapper around getopt_long that (ab)uses Key-Value Coding to simplify parsing options. But it also does things like setup the autorelease pool and provide handy wrappers around printf that work with the %@ format. Check out the extensive Doxygen documentation for examples and API reference. I think it makes writing command line applications a lot more painless.