Rewrite LightOptionParser to support multiple parameter arguments.
* LightOptionParser will only make one pass through the arguments array. * Make LOP support multiple instances of an option (-i in1 -i in2) * Make LOP support indeterminate option argument lengths (using arguments: "variable" in the definition). * Add unit tests for LightOptionParser and a testing phase during the `package` build target.
This commit is contained in:
parent
8ab35b27bd
commit
4ab4e5876b
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<import file="jdb-build-1.10.xml"/>
|
<import file="jdb-build-1.10.xml"/>
|
||||||
|
|
||||||
<target name="package" depends="-build-modular">
|
<target name="package" depends="run-tests,-build-modular">
|
||||||
<copy
|
<copy
|
||||||
file="${build.dir}/${name}-${version}.${build.number}.jar"
|
file="${build.dir}/${name}-${version}.${build.number}.jar"
|
||||||
tofile="${build.dir}/${name}-${version}.jar"/>
|
tofile="${build.dir}/${name}-${version}.jar"/>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#Thu, 25 Sep 2014 02:29:18 -0500
|
#Tue, 28 Oct 2014 01:58:09 -0500
|
||||||
name=jdb-util
|
name=jdb-util
|
||||||
version=2.4
|
version=3.0
|
||||||
lib.local=true
|
lib.local=true
|
||||||
|
|
||||||
build.number=1
|
build.number=1
|
||||||
|
@ -11,59 +11,97 @@ public class LightOptionParser {
|
|||||||
|
|
||||||
public static def parseOptions(def optionDefinitions, List args) {
|
public static def parseOptions(def optionDefinitions, List args) {
|
||||||
|
|
||||||
def returnOpts = [:]
|
def returnOpts = [args:[]]
|
||||||
def foundOpts = [:]
|
|
||||||
def optionArgIndices = []
|
|
||||||
|
|
||||||
/// Find all the options.
|
/// Look through each of the arguments to see if it is an option.
|
||||||
args.eachWithIndex { arg, idx ->
|
/// Note that we are manually advancing the index in the loop.
|
||||||
if (arg.startsWith('--')) foundOpts[arg.substring(2)] = [idx: idx]
|
for (int i = 0; i < args.size();) {
|
||||||
else if (arg.startsWith('-')) foundOpts[arg.substring(1)] = [idx: idx] }
|
|
||||||
|
|
||||||
/// Look for option arguments.
|
def retVal = false
|
||||||
foundOpts.each { foundName, optInfo ->
|
def optName = false
|
||||||
|
|
||||||
def retVal
|
if (args[i].startsWith('--')) optName = args[i].substring(2)
|
||||||
|
else if (args[i].startsWith('-')) optName = args[i].substring(1)
|
||||||
|
|
||||||
/// Find the definition for this option.
|
/// This was recognized as an option, try to find the definition
|
||||||
def optDef = optionDefinitions.find {
|
/// and read in any arguments.
|
||||||
it.key == foundName || it.value.longName == foundName }
|
if (optName) {
|
||||||
|
|
||||||
if (!optDef) throw new IllegalArgumentException(
|
/// Find the definition for this option.
|
||||||
"Unrecognized option: '${args[optInfo.idx]}.")
|
def optDef = optionDefinitions.find {
|
||||||
|
it.key == optName || it.value.longName == optName }
|
||||||
|
|
||||||
def optName = optDef.key
|
if (!optDef) throw new IllegalArgumentException(
|
||||||
optDef = optDef.value
|
"Unrecognized option: '${args[i]}'.")
|
||||||
|
|
||||||
/// Remember the option index for later.
|
optName = optDef.key
|
||||||
optionArgIndices << optInfo.idx
|
optDef = optDef.value
|
||||||
|
|
||||||
/// If there are no arguments, this is a flag.
|
/// If there are no arguments, this is a flag. Set the value
|
||||||
if ((optDef.arguments ?: 0) == 0) retVal = true
|
/// and advance the index.
|
||||||
|
if ((optDef.arguments ?: 0) == 0) { retVal = true; i++ }
|
||||||
|
|
||||||
/// Otherwise, read in the arguments
|
/// If there are a pre-determined number of arguments, read them
|
||||||
if (optDef.arguments && optDef.arguments > 0) {
|
/// in.
|
||||||
|
else if (optDef.arguments &&
|
||||||
|
optDef.arguments instanceof Number &&
|
||||||
|
optDef.arguments > 0) {
|
||||||
|
|
||||||
/// Not enough arguments left
|
retVal = []
|
||||||
if ((optInfo.idx + optDef.arguments) >= args.size()) {
|
|
||||||
throw new Exception("Option '${args[optInfo.idx]}' " +
|
|
||||||
"expects ${optDef.arguments} arguments.") }
|
|
||||||
|
|
||||||
int firstArgIdx = optInfo.idx + 1
|
/// Not enough arguments left
|
||||||
|
if ((i + optDef.arguments) >= args.size()) {
|
||||||
|
throw new Exception("Option '${args[i]}' " +
|
||||||
|
"expects ${optDef.arguments} arguments.") }
|
||||||
|
|
||||||
/// Case of only one argument
|
/// Advance past the option onto the first argument.
|
||||||
if (optDef.arguments == 1)
|
i++
|
||||||
retVal = args[firstArgIdx]
|
|
||||||
/// Case of multiple arguments
|
|
||||||
else retVal = args[firstArgIdx..<(firstArgIdx + optDef.arguments)]
|
|
||||||
|
|
||||||
/// Remember all the option argument indices for later.
|
/// Copy the arguments
|
||||||
(firstArgIdx..<(firstArgIdx + optDef.arguments)).each {
|
retVal += args[i..<(i + optDef.arguments)]
|
||||||
optionArgIndices << it }}
|
|
||||||
|
|
||||||
/// Store the value in the returnOpts map
|
/// Advance the index past end of the arguements
|
||||||
returnOpts[optName] = retVal
|
i += optDef.arguments }
|
||||||
if (optDef.longName) returnOpts[optDef.longName] = retVal }
|
|
||||||
|
/// If there are a variable number of arguments, treat all
|
||||||
|
/// arguments until the next argument or the end of options as
|
||||||
|
/// arguments for this option
|
||||||
|
else if (optDef.arguments == 'variable') {
|
||||||
|
|
||||||
|
retVal = []
|
||||||
|
|
||||||
|
/// Advance past the option to the first argument
|
||||||
|
i++
|
||||||
|
|
||||||
|
/// As long as we have not hit another option or the end of
|
||||||
|
/// arguments, keep adding arguments to the list for this
|
||||||
|
/// option.
|
||||||
|
for(;i < args.size() && !args[i].startsWith('-'); i++)
|
||||||
|
retVal << args[i] }
|
||||||
|
|
||||||
|
else {
|
||||||
|
throw new Exception("Invalid number of arguments " +
|
||||||
|
"defined for option ${optName}. The number of " +
|
||||||
|
"arguments must be either an integer or the value " +
|
||||||
|
"'variable'") }
|
||||||
|
|
||||||
|
/// Set the value on the option.
|
||||||
|
if (retVal instanceof Boolean) {
|
||||||
|
returnOpts[optName] = retVal
|
||||||
|
if (optDef.longName) returnOpts[optDef.longName] = retVal }
|
||||||
|
|
||||||
|
else {
|
||||||
|
if (!returnOpts.containsKey(optName))
|
||||||
|
returnOpts[optName] = []
|
||||||
|
returnOpts[optName] += retVal
|
||||||
|
|
||||||
|
if (optDef.longName) {
|
||||||
|
if (!returnOpts.containsKey(optDef.longName))
|
||||||
|
returnOpts[optDef.longName] = []
|
||||||
|
returnOpts[optDef.longName] += retVal } } }
|
||||||
|
|
||||||
|
/// This was not as option, it is an unclaomed argument.
|
||||||
|
else { returnOpts.args << args[i]; i++ } }
|
||||||
|
|
||||||
/// Check that all required options have been found.
|
/// Check that all required options have been found.
|
||||||
optionDefinitions.each { optName, optDef ->
|
optionDefinitions.each { optName, optDef ->
|
||||||
@ -71,17 +109,9 @@ public class LightOptionParser {
|
|||||||
if (optDef.required &&
|
if (optDef.required &&
|
||||||
/// and it has not been found, by either it's short or long name.
|
/// and it has not been found, by either it's short or long name.
|
||||||
!(returnOpts[optName] ||
|
!(returnOpts[optName] ||
|
||||||
(optDef.longName && returnOpts[longName])))
|
(optDef.longName && returnOpts[optDef.longName])))
|
||||||
|
|
||||||
throw new Exception("Missing required option: '-${optName}'.") }
|
throw new Exception("Missing required option: '-${optName}'.") }
|
||||||
|
|
||||||
/// Remove all the option arguments from the args list and return just
|
|
||||||
/// the non-option arguments.
|
|
||||||
optionArgIndices.sort().reverse().each { args.remove(it) }
|
|
||||||
|
|
||||||
//optionArgIndices = optionArgIndices.collect { args[it] }
|
|
||||||
//args.removeAll(optionArgIndices)
|
|
||||||
|
|
||||||
returnOpts.args = args
|
|
||||||
return returnOpts }
|
return returnOpts }
|
||||||
}
|
}
|
||||||
|
65
src/test/com/jdbernard/util/LightOptionParserTests.groovy
Normal file
65
src/test/com/jdbernard/util/LightOptionParserTests.groovy
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package com.jdbernard.util
|
||||||
|
|
||||||
|
import groovy.util.GroovyTestCase
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
import static com.jdbernard.util.LightOptionParser.parseOptions
|
||||||
|
|
||||||
|
public class LightOptionParserTests extends GroovyTestCase {
|
||||||
|
|
||||||
|
def helpDef = ['h': [longName: 'help']]
|
||||||
|
def confDef = ['c': [longName: 'config', required: true, arguments: 1]]
|
||||||
|
def fullDef = [
|
||||||
|
h: [longName: 'help'],
|
||||||
|
c: [longName: 'config-file', required: true, arguments: 1],
|
||||||
|
i: [longName: 'input-file', arguments: 'variable'],
|
||||||
|
o: [longName: 'output-file2', arguments: 2]]
|
||||||
|
|
||||||
|
void testShortFlagPresent1() { assert parseOptions(helpDef, ["-h"]).h }
|
||||||
|
void testShortFlagPresent2() { assert parseOptions(helpDef, ["-h"]).help }
|
||||||
|
void testLongFlagPresent() { assert parseOptions(helpDef, ["--help"]).h}
|
||||||
|
void testShortFlagPresent() { assert parseOptions(helpDef, ["--help"]).help }
|
||||||
|
|
||||||
|
void testFlagAbsent1() { assert !parseOptions(helpDef, ["arg"]).h }
|
||||||
|
void testFlagAbsent2() { assert !parseOptions(helpDef, ["arg"]).help }
|
||||||
|
|
||||||
|
void testRequiredOptionMissing() {
|
||||||
|
try {
|
||||||
|
parseOptions(confDef, ["arg"])
|
||||||
|
assert false }
|
||||||
|
catch (Exception e) {} }
|
||||||
|
|
||||||
|
void testSingleArg1() {
|
||||||
|
assert parseOptions(confDef, ["-c", "confFile"]).c == ["confFile"] }
|
||||||
|
|
||||||
|
void testSingleArg2() {
|
||||||
|
assert parseOptions(confDef, ["-c", "confFile"]).config == ["confFile"] }
|
||||||
|
|
||||||
|
void testUnclaimedArgsAndFlag() {
|
||||||
|
def opts = parseOptions(helpDef, ["arg1", "-h", "arg2"])
|
||||||
|
assert opts.args == ["arg1", "arg2"] }
|
||||||
|
|
||||||
|
void testUnclaimedAndClaimedArgs() {
|
||||||
|
def opts = parseOptions(fullDef, ["-c", "confFile", "arg1"])
|
||||||
|
assert opts.args == ["arg1"]
|
||||||
|
assert opts.c == ["confFile"] }
|
||||||
|
|
||||||
|
/*void testMultipleArgs1() {
|
||||||
|
def opts = parseOptions(fullDef, ["-c", "confFile", ""])
|
||||||
|
assert .conf == ["confFile"] }*/
|
||||||
|
|
||||||
|
void testFull() {
|
||||||
|
def opts = parseOptions(fullDef,
|
||||||
|
["-c", "cfgFile", "arg1", "-i", "in1", "in2", "in3",
|
||||||
|
"-o", "out1", "out2", "arg2", "-h", "-i", "in4"])
|
||||||
|
|
||||||
|
assert opts.h
|
||||||
|
assert opts.c == ["cfgFile"]
|
||||||
|
assert opts['config-file'] == ["cfgFile"]
|
||||||
|
assert opts.args == ["arg1", "arg2"]
|
||||||
|
assert opts.i == ["in1", "in2", "in3", "in4"]
|
||||||
|
assert opts["input-file"] == ["in1", "in2", "in3", "in4"]
|
||||||
|
assert opts.o == ["out1", "out2"]
|
||||||
|
assert opts["output-file2"] == ["out1", "out2"] }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user