You are currently browsing the Tom Malaher’s BrainScan weblog archives for the day June 7, 2004.
- Bookmarks (147)
- Books (1)
- Fr0n (1)
- LifeInGeneral (2)
- On the trail (4)
- Photos (4)
- Practice (7)
- Software (10)
- Tools (9)
- July 10, 2011: links for 2011-07-10
- July 4, 2011: links for 2011-07-04
- June 22, 2011: links for 2011-06-22
- May 17, 2011: links for 2011-05-17
- May 16, 2011: links for 2011-05-16
- May 12, 2011: links for 2011-05-12
- May 10, 2011: links for 2011-05-10
- April 28, 2011: links for 2011-04-28
- April 23, 2011: links for 2011-04-23
- April 12, 2011: links for 2011-04-12
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- April 2009
- March 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- May 2007
- April 2007
- March 2007
- December 2006
- May 2006
- June 2004
- May 2004
- February 2004
Archive for June 7, 2004
Unix command line utility program conventions
June 7, 2004 by Tom Malaher.
Sometimes a vendor supplies a command-line utility for performing some function that we want to use from within our scripts and programs.
There are some unwritten (at least as far as I can find) rules about how to write one of these utilities so it can be used properly.
Some vendors get this right. Others, not so much…
The Rules
Return an error status indicating success or failure. For bonus points, return multiple different error codes depending on what went wrong. (The Anna Karenina Principle)
And you know what? That’s about it. The rest (arguments, input/output locations, etc., etc.) really depends on the context and function of the item in quesiton. Though the following are useful:
- Provide a useful usage message if invalid arguments are passed.
- Provide an explict way (e.g. –help option) to ask for the above usage message.
- Use GNU-style arguments.
But these are really for human consumption, not for use in a script.
Recent Failures
I won’t name names, but here are some of the failures I’ve seen recently. (And these are from Big Companies that should know better. Including one company that actually has its own version of Unix… they should really know better.)
- utility always returns status 1. How the HECK am I supposed to know if it worked? Why are you always returning failure? Didn’t you read a single Unix man page? Didn’t you notice that non-zero exit codes mean failure?
- -q option suppresses error messages to stdout/stderr… and suppresses the error code return as well. Take a look at diff(1) sometime. The -q option just suppresses the listing of the differences, but still returns the error code.
Strangely enough, these rules apply equally well to Windows command-line utilities. Yes, these do exist.
Another suggestion
If it can be done with a command-line utiltiy, then give us an API we can use.
If you even just create a simple C library, we can then wrap it into our favorite language as a Perl Extension Module, or a Java Native Interface (JNI) pacakge.
If you feel like creating a pure Java implementation of the Library, that would be good too.
From Dan Moore:
On a different tack, but still touching some of the same principles, you may want to check out the Command-Line Options section of the Art of Unix Programming
Originally Posted June 7, 2004 11:20 AM
Posted in Practice, Software | Print | No Comments »