GOP (groovy option parser) v0.9 available
Subheadline:
A simple to use command line option parser
GOP is a command line option parsing alternative to CliBuilder
An example is the easiest explanation
def parser = new org.computoring.gop.Parser(description: "An example parser.")
parser.with {
required 'f', 'foo-bar', [description: 'The foo-bar option']
optional 'b', [
longName: 'bar-baz',
default: 'xyz',
description: 'The optional bar-baz option with a default of "xyz"'
]
flag 'c'
flag 'd', 'debug', [default: true]
required 'i', 'count', [
description: 'A required, validated option',
validate: {
Integer.parseInt it
}
]
// require a remainder
remainder {
if(!it) throw new IllegalArgumentException("missing command")
it
}
}
def params = parser.parse("-f foo_value --debug --count 123 -- some other stuff".split())
assert params.'foo-bar' == 'foo_value'
assert params.b == 'xyz'
assert params.c == false
assert params.debug == true
assert params.count instanceof Integer
assert params.i == 123
assert parser.remainder.join(' ') == 'some other stuff'
System.out << parser.usage // will spit out a usage statement like this:
An example parser.
Required
-f, --foo-bar The foo-bar option
-i, --count A required, validated option
Optional
-b, --bar-baz [xyz] The optional bar-baz option with a default of "xyz"
Flags
-c [false]
-d, --debug [true]
A couple interesting features
- Flag and optional options can have default values
- Options can specify a validation closure that parsed values are passed through
- remaining parameters after parsing can be validated
- Formatted usage statement is created for you.
Location:
http://code.google.com/p/groovy-option-parser/
(2 votes)
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




