views:

38

answers:

2

I'm currently writing some Java to wrap around an extensive command line tool. It feels like I'm writing a lot of similar code.

I'm wondering if there are any established patterns for wrapping command line tools - passing arguments and handling output and so on.

Specific examples in Java would obviously be great, but any general suggestions or pointers are welcome too.

+1  A: 

Apache Commons Exec might be of some help to you. This is a little library aimed at simplyfying executing of external processes in Java.

There is also Apache Commons CLI for dealing with command line parsing if you need an extensive support for one.

pajton
+1  A: 

If you are primarily wrapping a command line tool, then you might want to consider using groovy instead of or in addition to Java. Executing a command in groovy is a simple as:

def p = 'my command'.execute()

print p.text //prints output

You could also chain these methods together like this:

print 'my command'.execute().text

Ken Liu