Skip to content

Commit 24972c5

Browse files
author
John Keyes
committed
- improved code coverage
- added leniant date parsing to DateValidator git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@280577 13f79535-47bb-0310-9956-ffa450edef68
1 parent 234ae35 commit 24972c5

11 files changed

Lines changed: 827 additions & 848 deletions

File tree

src/java/org/apache/commons/cli2/option/ArgumentImpl.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package org.apache.commons.cli2.option;
1717

18+
import java.util.ArrayList;
1819
import java.util.Collections;
1920
import java.util.Comparator;
21+
import java.util.Iterator;
2022
import java.util.List;
2123
import java.util.ListIterator;
2224
import java.util.Set;
@@ -335,13 +337,7 @@ public String stripBoundaryQuotes(String token) {
335337
return token;
336338
}
337339

338-
if (token.startsWith("\"")) {
339-
token = token.substring(1, token.length());
340-
}
341-
342-
if (token.endsWith("\"")) {
343-
token = token.substring(0, token.length() - 1);
344-
}
340+
token = token.substring(1, token.length() - 1);
345341

346342
return token;
347343
}

src/java/org/apache/commons/cli2/option/GroupImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public GroupImpl(final List options,
110110
}
111111

112112
public boolean canProcess(final WriteableCommandLine commandLine,
113-
String arg) {
113+
final String arg) {
114114
if (arg == null) {
115115
return false;
116116
}

src/java/org/apache/commons/cli2/option/ParentImpl.java

Lines changed: 63 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright 2003-2005 The Apache Software Foundation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -34,39 +34,33 @@
3434
* A base implementation of Parent providing limited ground work for further
3535
* Parent implementations.
3636
*/
37-
public abstract class ParentImpl extends OptionImpl implements Parent {
38-
37+
public abstract class ParentImpl
38+
extends OptionImpl implements Parent {
3939
private static final char NUL = '\0';
40-
4140
private final Group children;
42-
4341
private final Argument argument;
44-
4542
private final String description;
4643

47-
protected ParentImpl(
48-
final Argument argument,
49-
final Group children,
50-
final String description,
51-
final int id,
52-
final boolean required) {
53-
super(id,required);
44+
protected ParentImpl(final Argument argument,
45+
final Group children,
46+
final String description,
47+
final int id,
48+
final boolean required) {
49+
super(id, required);
5450
this.children = children;
5551
this.argument = argument;
5652
this.description = description;
5753
}
5854

5955
/*
6056
* (non-Javadoc)
61-
*
57+
*
6258
* @see org.apache.commons.cli2.Option#process(org.apache.commons.cli2.CommandLine,
6359
* java.util.ListIterator)
6460
*/
65-
public void process(
66-
final WriteableCommandLine commandLine,
67-
final ListIterator arguments)
61+
public void process(final WriteableCommandLine commandLine,
62+
final ListIterator arguments)
6863
throws OptionException {
69-
7064
if (argument != null) {
7165
handleInitialSeparator(arguments, argument.getInitialSeparator());
7266
}
@@ -77,51 +71,49 @@ public void process(
7771
argument.processValues(commandLine, arguments, this);
7872
}
7973

80-
if (children != null && children.canProcess(commandLine, arguments)) {
74+
if ((children != null) && children.canProcess(commandLine, arguments)) {
8175
children.process(commandLine, arguments);
8276
}
8377
}
8478

8579
/*
8680
* (non-Javadoc)
87-
*
81+
*
8882
* @see org.apache.commons.cli2.Option#canProcess(java.lang.String)
8983
*/
90-
public boolean canProcess(final WriteableCommandLine commandLine, final String arg) {
91-
84+
public boolean canProcess(final WriteableCommandLine commandLine,
85+
final String arg) {
9286
final Set triggers = getTriggers();
93-
87+
9488
if (argument != null) {
9589
final char separator = argument.getInitialSeparator();
96-
90+
9791
// if there is a valid separator character
9892
if (separator != NUL) {
9993
final int initialIndex = arg.indexOf(separator);
100-
94+
10195
// if there is a separator present
10296
if (initialIndex > 0) {
10397
return triggers.contains(arg.substring(0, initialIndex));
10498
}
10599
}
106100
}
107-
101+
108102
return triggers.contains(arg);
109103
}
110104

111105
/*
112106
* (non-Javadoc)
113-
*
107+
*
114108
* @see org.apache.commons.cli2.Option#prefixes()
115109
*/
116110
public Set getPrefixes() {
117-
return (children == null)
118-
? Collections.EMPTY_SET
119-
: children.getPrefixes();
111+
return (children == null) ? Collections.EMPTY_SET : children.getPrefixes();
120112
}
121113

122114
/*
123115
* (non-Javadoc)
124-
*
116+
*
125117
* @see org.apache.commons.cli2.Option#validate(org.apache.commons.cli2.CommandLine)
126118
*/
127119
public void validate(WriteableCommandLine commandLine)
@@ -139,21 +131,19 @@ public void validate(WriteableCommandLine commandLine)
139131

140132
/*
141133
* (non-Javadoc)
142-
*
134+
*
143135
* @see org.apache.commons.cli2.Option#appendUsage(java.lang.StringBuffer,
144136
* java.util.Set, java.util.Comparator)
145137
*/
146-
public void appendUsage(
147-
final StringBuffer buffer,
148-
final Set helpSettings,
149-
final Comparator comp) {
150-
138+
public void appendUsage(final StringBuffer buffer,
139+
final Set helpSettings,
140+
final Comparator comp) {
151141
final boolean displayArgument =
152-
this.argument != null
153-
&& helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
142+
(this.argument != null) &&
143+
helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
154144
final boolean displayChildren =
155-
this.children != null
156-
&& helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN);
145+
(this.children != null) &&
146+
helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN);
157147

158148
if (displayArgument) {
159149
buffer.append(' ');
@@ -175,24 +165,21 @@ public String getDescription() {
175165

176166
/*
177167
* (non-Javadoc)
178-
*
168+
*
179169
* @see org.apache.commons.cli2.Option#helpLines(int, java.util.Set,
180170
* java.util.Comparator)
181171
*/
182-
public List helpLines(
183-
final int depth,
184-
final Set helpSettings,
185-
final Comparator comp) {
172+
public List helpLines(final int depth,
173+
final Set helpSettings,
174+
final Comparator comp) {
186175
final List helpLines = new ArrayList();
187176
helpLines.add(new HelpLineImpl(this, depth));
188177

189-
if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT)
190-
&& argument != null) {
178+
if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT) && (argument != null)) {
191179
helpLines.addAll(argument.helpLines(depth + 1, helpSettings, comp));
192180
}
193181

194-
if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN)
195-
&& children != null) {
182+
if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN) && (children != null)) {
196183
helpLines.addAll(children.helpLines(depth + 1, helpSettings, comp));
197184
}
198185

@@ -218,46 +205,45 @@ public Group getChildren() {
218205
* @param arguments the current position in the arguments iterator
219206
* @param separator the separator char to split on
220207
*/
221-
private void handleInitialSeparator(
222-
final ListIterator arguments,
223-
final char separator) {
224-
208+
private void handleInitialSeparator(final ListIterator arguments,
209+
final char separator) {
225210
// next token
226-
final String newArgument = (String)arguments.next();
227-
211+
final String newArgument = (String) arguments.next();
212+
228213
// split the token
229214
final int initialIndex = newArgument.indexOf(separator);
230-
215+
231216
if (initialIndex > 0) {
232217
arguments.remove();
233218
arguments.add(newArgument.substring(0, initialIndex));
234219
arguments.add(newArgument.substring(initialIndex + 1));
235220
arguments.previous();
236221
}
237-
arguments.previous();
222+
223+
arguments.previous();
224+
}
225+
226+
/*
227+
* @see org.apache.commons.cli2.Option#findOption(java.lang.String)
228+
*/
229+
public Option findOption(final String trigger) {
230+
final Option found = super.findOption(trigger);
231+
232+
if ((found == null) && (children != null)) {
233+
return children.findOption(trigger);
234+
} else {
235+
return found;
236+
}
238237
}
239-
240-
/*
241-
* @see org.apache.commons.cli2.Option#findOption(java.lang.String)
242-
*/
243-
public Option findOption(final String trigger) {
244-
final Option found = super.findOption(trigger);
245-
if(found==null && children!=null){
246-
return children.findOption(trigger);
247-
}
248-
else{
249-
return found;
250-
}
251-
}
252-
238+
253239
public void defaults(final WriteableCommandLine commandLine) {
254240
super.defaults(commandLine);
255-
256-
if(argument!=null) {
257-
argument.defaultValues(commandLine,this);
241+
242+
if (argument != null) {
243+
argument.defaultValues(commandLine, this);
258244
}
259-
260-
if(children!=null) {
245+
246+
if (children != null) {
261247
children.defaults(commandLine);
262248
}
263249
}

src/java/org/apache/commons/cli2/validation/DateValidator.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.text.ParsePosition;
2020

2121
import java.util.Date;
22+
import java.util.Iterator;
2223
import java.util.List;
2324
import java.util.ListIterator;
2425

@@ -72,6 +73,9 @@ public class DateValidator implements Validator {
7273
/** maximum Date allowed i.e: a valid date occurs earlier than this date */
7374
private Date maximum;
7475

76+
/** leniant parsing */
77+
private boolean isLenient;
78+
7579
/**
7680
* Creates a Validator for the default date/time format
7781
*/
@@ -96,6 +100,10 @@ public DateValidator(final DateFormat format) {
96100
* a List of DateFormats which dates must conform to
97101
*/
98102
public DateValidator(final List formats) {
103+
for (Iterator iter = formats.iterator(); iter.hasNext();) {
104+
DateFormat format = (DateFormat) iter.next();
105+
}
106+
99107
setFormats(formats);
100108
}
101109

@@ -150,10 +158,6 @@ public void validate(final List values)
150158
for (int f = 0; (f < this.formats.length) && (date == null); ++f) {
151159
// reset the parse position
152160
pp.setIndex(0);
153-
154-
// TODO: should we call setLenient(false) on
155-
// each DateFormat or allow the user
156-
// to specify the parsing used
157161
date = this.formats[f].parse(value, pp);
158162

159163
// if the wrong number of characters have been parsed
@@ -178,6 +182,18 @@ public void validate(final List values)
178182
}
179183
}
180184

185+
public void setLeniant(final boolean lenient) {
186+
for (int i = 0; i < this.formats.length; i++) {
187+
this.formats[i].setLenient(lenient);
188+
}
189+
190+
this.isLenient = lenient;
191+
}
192+
193+
public boolean isLeniant() {
194+
return this.isLenient;
195+
}
196+
181197
/**
182198
* Returns the maximum date permitted.
183199
*
@@ -272,6 +288,7 @@ public void setFormats(final List formats) {
272288
*/
273289
public void setFormats(final DateFormat[] formats) {
274290
this.formats = formats;
291+
setLeniant(this.isLenient);
275292
}
276293

277294
/**

src/test/org/apache/commons/cli2/commandline/DefaultingCommandLineTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626

2727
/**
2828
* @author Rob Oxspring
29-
*
30-
* To change the template for this generated type comment go to
31-
* Window - Preferences - Java - Code Generation - Code and Comments
3229
*/
3330
public class DefaultingCommandLineTest
3431
extends CommandLineTestCase {

0 commit comments

Comments
 (0)