Skip to content

Commit a2cf820

Browse files
committed
Added the missing parse methods accepting a map with the default options to DefaultParser
Moved the related tests from ValueTest to ParserTestCase git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@955099 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3433f46 commit a2cf820

3 files changed

Lines changed: 203 additions & 104 deletions

File tree

src/java/org/apache/commons/cli/DefaultParser.java

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package org.apache.commons.cli;
1919

2020
import java.util.ArrayList;
21-
import java.util.List;
21+
import java.util.Enumeration;
2222
import java.util.Iterator;
23+
import java.util.List;
24+
import java.util.Properties;
2325

2426
/**
2527
* Default parser.
@@ -54,10 +56,46 @@ public class DefaultParser implements CommandLineParser
5456

5557
public CommandLine parse(Options options, String[] arguments) throws ParseException
5658
{
57-
return parse(options, arguments, false);
59+
return parse(options, arguments, null);
60+
}
61+
62+
/**
63+
* Parse the arguments according to the specified options and properties.
64+
*
65+
* @param options the specified Options
66+
* @param arguments the command line arguments
67+
* @param properties command line option name-value pairs
68+
* @return the list of atomic option and value tokens
69+
*
70+
* @throws ParseException if there are any problems encountered
71+
* while parsing the command line tokens.
72+
*/
73+
public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException
74+
{
75+
return parse(options, arguments, properties, false);
5876
}
5977

6078
public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException
79+
{
80+
return parse(options, arguments, null, stopAtNonOption);
81+
}
82+
83+
/**
84+
* Parse the arguments according to the specified options and properties.
85+
*
86+
* @param options the specified Options
87+
* @param arguments the command line arguments
88+
* @param properties command line option name-value pairs
89+
* @param stopAtNonOption if <tt>true</tt> an unrecognized argument stops
90+
* the parsing and the remaining arguments are added to the
91+
* {@link CommandLine}s args list. If <tt>false</tt> an unrecognized
92+
* argument triggers a ParseException.
93+
*
94+
* @return the list of atomic option and value tokens
95+
* @throws ParseException if there are any problems encountered
96+
* while parsing the command line tokens.
97+
*/
98+
public CommandLine parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) throws ParseException
6199
{
62100
this.options = options;
63101
this.stopAtNonOption = stopAtNonOption;
@@ -85,11 +123,57 @@ public CommandLine parse(Options options, String[] arguments, boolean stopAtNonO
85123
// check the arguments of the last option
86124
checkRequiredArgs();
87125

126+
// add the default options
127+
handleProperties(properties);
128+
88129
checkRequiredOptions();
89130

90131
return cmd;
91132
}
92133

134+
/**
135+
* Sets the values of Options using the values in <code>properties</code>.
136+
*
137+
* @param properties The value properties to be processed.
138+
*/
139+
private void handleProperties(Properties properties)
140+
{
141+
if (properties == null)
142+
{
143+
return;
144+
}
145+
146+
for (Enumeration e = properties.propertyNames(); e.hasMoreElements();)
147+
{
148+
String option = e.nextElement().toString();
149+
150+
if (!cmd.hasOption(option))
151+
{
152+
Option opt = options.getOption(option);
153+
154+
// get the value from the properties
155+
String value = properties.getProperty(option);
156+
157+
if (opt.hasArg())
158+
{
159+
if (opt.getValues() == null || opt.getValues().length == 0)
160+
{
161+
opt.addValueForProcessing(value);
162+
}
163+
}
164+
else if (!("yes".equalsIgnoreCase(value)
165+
|| "true".equalsIgnoreCase(value)
166+
|| "1".equalsIgnoreCase(value)))
167+
{
168+
// if the value is not yes, true or 1 then don't add the option to the CommandLine
169+
continue;
170+
}
171+
172+
cmd.addOption(opt);
173+
}
174+
}
175+
}
176+
93177
/**
94178
* Throws a {@link MissingOptionException} if all of the required options
95179
* are not present.

src/test/org/apache/commons/cli/ParserTestCase.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,4 +869,121 @@ public void testUnlimitedArgs() throws Exception
869869
assertTrue("Confirm -f is set", cl.hasOption("f"));
870870
assertEquals("number of arg for -f", 1, cl.getOptionValues("f").length);
871871
}
872+
873+
private CommandLine parse(CommandLineParser parser, Options opts, String[] args, Properties properties) throws ParseException {
874+
if (parser instanceof Parser) {
875+
return ((Parser) parser).parse(opts, args, properties);
876+
} else if (parser instanceof DefaultParser) {
877+
return ((DefaultParser) parser).parse(opts, args, properties);
878+
} else {
879+
throw new UnsupportedOperationException("Default options not supported by this parser");
880+
}
881+
}
882+
883+
public void testPropertyOptionSingularValue() throws Exception
884+
{
885+
Options opts = new Options();
886+
opts.addOption(OptionBuilder.hasOptionalArgs(2).withLongOpt("hide").create());
887+
888+
Properties properties = new Properties();
889+
properties.setProperty( "hide", "seek" );
890+
891+
CommandLine cmd = parse(parser, opts, null, properties);
892+
assertTrue( cmd.hasOption("hide") );
893+
assertEquals( "seek", cmd.getOptionValue("hide") );
894+
assertTrue( !cmd.hasOption("fake") );
895+
}
896+
897+
public void testPropertyOptionFlags() throws Exception
898+
{
899+
Options opts = new Options();
900+
opts.addOption("a", false, "toggle -a");
901+
opts.addOption("c", "c", false, "toggle -c");
902+
opts.addOption(OptionBuilder.hasOptionalArg().create('e'));
903+
904+
Properties properties = new Properties();
905+
properties.setProperty("a", "true");
906+
properties.setProperty("c", "yes");
907+
properties.setProperty("e", "1");
908+
909+
CommandLine cmd = parse(parser, opts, null, properties);
910+
assertTrue(cmd.hasOption("a"));
911+
assertTrue(cmd.hasOption("c"));
912+
assertTrue(cmd.hasOption("e"));
913+
914+
915+
properties = new Properties();
916+
properties.setProperty("a", "false");
917+
properties.setProperty("c", "no");
918+
properties.setProperty("e", "0");
919+
920+
cmd = parse(parser, opts, null, properties);
921+
assertTrue(!cmd.hasOption("a"));
922+
assertTrue(!cmd.hasOption("c"));
923+
assertTrue(cmd.hasOption("e")); // this option accepts an argument
924+
925+
926+
properties = new Properties();
927+
properties.setProperty("a", "TRUE");
928+
properties.setProperty("c", "nO");
929+
properties.setProperty("e", "TrUe");
930+
931+
cmd = parse(parser, opts, null, properties);
932+
assertTrue(cmd.hasOption("a"));
933+
assertTrue(!cmd.hasOption("c"));
934+
assertTrue(cmd.hasOption("e"));
935+
936+
937+
properties = new Properties();
938+
properties.setProperty("a", "just a string");
939+
properties.setProperty("e", "");
940+
941+
cmd = parse(parser, opts, null, properties);
942+
assertTrue(!cmd.hasOption("a"));
943+
assertTrue(!cmd.hasOption("c"));
944+
assertTrue(cmd.hasOption("e"));
945+
946+
947+
properties = new Properties();
948+
properties.setProperty("a", "0");
949+
properties.setProperty("c", "1");
950+
951+
cmd = parse(parser, opts, null, properties);
952+
assertTrue(!cmd.hasOption("a"));
953+
assertTrue(cmd.hasOption("c"));
954+
}
955+
956+
public void testPropertyOptionMultipleValues() throws Exception
957+
{
958+
Options opts = new Options();
959+
opts.addOption(OptionBuilder.hasArgs().withValueSeparator(',').create('k'));
960+
961+
Properties properties = new Properties();
962+
properties.setProperty( "k", "one,two" );
963+
964+
String[] values = new String[] { "one", "two" };
965+
966+
CommandLine cmd = parse(parser, opts, null, properties);
967+
assertTrue( cmd.hasOption("k") );
968+
assertTrue( Arrays.equals( values, cmd.getOptionValues('k') ) );
969+
}
970+
971+
public void testPropertyOverrideValues() throws Exception
972+
{
973+
Options opts = new Options();
974+
opts.addOption(OptionBuilder.hasOptionalArgs(2).create('i'));
975+
opts.addOption(OptionBuilder.hasOptionalArgs().create('j'));
976+
977+
String[] args = new String[] { "-j", "found", "-i", "ink" };
978+
979+
Properties properties = new Properties();
980+
properties.setProperty( "j", "seek" );
981+
982+
CommandLine cmd = parse(parser, opts, args, properties);
983+
assertTrue( cmd.hasOption("j") );
984+
assertEquals( "found", cmd.getOptionValue("j") );
985+
assertTrue( cmd.hasOption("i") );
986+
assertEquals( "ink", cmd.getOptionValue("i") );
987+
assertTrue( !cmd.hasOption("fake") );
988+
}
872989
}

src/test/org/apache/commons/cli/ValueTest.java

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package org.apache.commons.cli;
1919

20-
import java.util.Arrays;
21-
import java.util.Properties;
22-
2320
import junit.framework.TestCase;
2421

2522
public class ValueTest extends TestCase
@@ -40,7 +37,6 @@ public void setUp() throws Exception
4037
opts.addOption(OptionBuilder.hasOptionalArgs(2).withLongOpt("hide").create());
4138
opts.addOption(OptionBuilder.hasOptionalArgs(2).create('i'));
4239
opts.addOption(OptionBuilder.hasOptionalArgs().create('j'));
43-
opts.addOption(OptionBuilder.hasArgs().withValueSeparator(',').create('k'));
4440

4541
String[] args = new String[] { "-a",
4642
"-b", "foo",
@@ -175,102 +171,4 @@ public void testLongOptionalNArgValues() throws Exception
175171
assertEquals( cmd.getArgs().length, 1 );
176172
assertEquals( "head", cmd.getArgs()[0] );
177173
}
178-
179-
public void testPropertyOptionSingularValue() throws Exception
180-
{
181-
Properties properties = new Properties();
182-
properties.setProperty( "hide", "seek" );
183-
184-
Parser parser = new PosixParser();
185-
186-
CommandLine cmd = parser.parse(opts, null, properties);
187-
assertTrue( cmd.hasOption("hide") );
188-
assertEquals( "seek", cmd.getOptionValue("hide") );
189-
assertTrue( !cmd.hasOption("fake") );
190-
}
191-
192-
public void testPropertyOptionFlags() throws Exception
193-
{
194-
Properties properties = new Properties();
195-
properties.setProperty( "a", "true" );
196-
properties.setProperty( "c", "yes" );
197-
properties.setProperty( "e", "1" );
198-
199-
Parser parser = new PosixParser();
200-
201-
CommandLine cmd = parser.parse(opts, null, properties);
202-
assertTrue( cmd.hasOption("a") );
203-
assertTrue( cmd.hasOption("c") );
204-
assertTrue( cmd.hasOption("e") );
205-
206-
207-
properties = new Properties();
208-
properties.setProperty( "a", "false" );
209-
properties.setProperty( "c", "no" );
210-
properties.setProperty( "e", "0" );
211-
212-
cmd = parser.parse(opts, null, properties);
213-
assertTrue( !cmd.hasOption("a") );
214-
assertTrue( !cmd.hasOption("c") );
215-
assertTrue( cmd.hasOption("e") ); // this option accepts as argument
216-
217-
218-
properties = new Properties();
219-
properties.setProperty( "a", "TRUE" );
220-
properties.setProperty( "c", "nO" );
221-
properties.setProperty( "e", "TrUe" );
222-
223-
cmd = parser.parse(opts, null, properties);
224-
assertTrue( cmd.hasOption("a") );
225-
assertTrue( !cmd.hasOption("c") );
226-
assertTrue( cmd.hasOption("e") );
227-
228-
229-
properties = new Properties();
230-
properties.setProperty( "a", "just a string" );
231-
properties.setProperty( "e", "" );
232-
233-
cmd = parser.parse(opts, null, properties);
234-
assertTrue( !cmd.hasOption("a") );
235-
assertTrue( !cmd.hasOption("c") );
236-
assertTrue( cmd.hasOption("e") );
237-
}
238-
239-
public void testPropertyOptionMultipleValues() throws Exception
240-
{
241-
Properties properties = new Properties();
242-
properties.setProperty( "k", "one,two" );
243-
244-
Parser parser = new PosixParser();
245-
246-
String[] values = new String[] {
247-
"one", "two"
248-
};
249-
250-
CommandLine cmd = parser.parse(opts, null, properties);
251-
assertTrue( cmd.hasOption("k") );
252-
assertTrue( Arrays.equals( values, cmd.getOptionValues('k') ) );
253-
}
254-
255-
public void testPropertyOverrideValues() throws Exception
256-
{
257-
String[] args = new String[] {
258-
"-j",
259-
"found",
260-
"-i",
261-
"ink"
262-
};
263-
264-
Properties properties = new Properties();
265-
properties.setProperty( "j", "seek" );
266-
267-
Parser parser = new PosixParser();
268-
CommandLine cmd = parser.parse(opts, args, properties);
269-
assertTrue( cmd.hasOption("j") );
270-
assertEquals( "found", cmd.getOptionValue("j") );
271-
assertTrue( cmd.hasOption("i") );
272-
assertEquals( "ink", cmd.getOptionValue("i") );
273-
assertTrue( !cmd.hasOption("fake") );
274-
}
275-
276174
}

0 commit comments

Comments
 (0)