1+ /**
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright ownership.
5+ * The ASF licenses this file to You under the Apache License, Version 2.0
6+ * (the "License"); you may not use this file except in compliance with
7+ * the License. You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+ package org .apache .commons .cli2 .bug ;
18+
19+ import java .util .Arrays ;
20+ import java .util .List ;
21+
22+ import junit .framework .TestCase ;
23+
24+ import org .apache .commons .cli2 .CommandLine ;
25+ import org .apache .commons .cli2 .Group ;
26+ import org .apache .commons .cli2 .builder .ArgumentBuilder ;
27+ import org .apache .commons .cli2 .builder .DefaultOptionBuilder ;
28+ import org .apache .commons .cli2 .builder .GroupBuilder ;
29+ import org .apache .commons .cli2 .commandline .Parser ;
30+ import org .apache .commons .cli2 .option .DefaultOption ;
31+
32+ /**
33+ * http://issues.apache.org/jira/browse/CLI-158
34+ */
35+ public class BugCLI158Test extends TestCase {
36+
37+ private Parser createDefaultValueParser (String [] defaults ) {
38+ final DefaultOptionBuilder obuilder = new DefaultOptionBuilder ();
39+ final ArgumentBuilder abuilder = new ArgumentBuilder ();
40+ final GroupBuilder gbuilder = new GroupBuilder ();
41+
42+ DefaultOption bOption = obuilder .withShortName ("b" )
43+ .withLongName ("b" )
44+ .withArgument (abuilder .withName ("b" )
45+ .withMinimum (0 )
46+ .withMaximum (defaults .length )
47+ .withDefaults (Arrays .asList (defaults ))
48+ .create ())
49+ .create ();
50+
51+ Group options = gbuilder
52+ .withName ("options" )
53+ .withOption (bOption )
54+ .create ();
55+
56+ Parser parser = new Parser ();
57+ parser .setHelpTrigger ("--help" );
58+ parser .setGroup (options );
59+ return parser ;
60+ }
61+
62+ public void testSingleOptionSingleArgument () throws Exception {
63+ Parser parser = createDefaultValueParser (new String []{"100" , "1000" });
64+ String enteredValue1 = "1" ;
65+ String [] args = new String []{"-b" , enteredValue1 };
66+ CommandLine cl = parser .parse (args );
67+ CommandLine cmd = cl ;
68+ assertNotNull (cmd );
69+ List b = cmd .getValues ("-b" );
70+ assertEquals ("[" + enteredValue1 + ", 1000]" , b + "" );
71+ }
72+
73+ public void testSingleOptionNoArgument () throws Exception {
74+ Parser parser = createDefaultValueParser (new String []{"100" , "1000" });
75+ String [] args = new String []{"-b" };
76+ CommandLine cl = parser .parse (args );
77+ CommandLine cmd = cl ;
78+ assertNotNull (cmd );
79+ List b = cmd .getValues ("-b" );
80+ assertEquals ("[100, 1000]" , b + "" );
81+ }
82+
83+ public void testSingleOptionMaximumNumberOfArgument () throws Exception {
84+ String [] args = new String []{"-b" , "1" , "2" };
85+ final ArgumentBuilder abuilder = new ArgumentBuilder ();
86+ final DefaultOptionBuilder obuilder = new DefaultOptionBuilder ();
87+ final GroupBuilder gbuilder = new GroupBuilder ();
88+
89+ DefaultOption bOption = obuilder .withShortName ("b" )
90+ .withLongName ("b" )
91+ .withArgument (abuilder .withName ("b" )
92+ .withMinimum (2 )
93+ .withMaximum (4 )
94+ .withDefault ("100" )
95+ .withDefault ("1000" )
96+ .withDefault ("10000" )
97+ .create ())
98+ .create ();
99+
100+ Group options = gbuilder
101+ .withName ("options" )
102+ .withOption (bOption )
103+ .create ();
104+
105+ Parser parser = new Parser ();
106+ parser .setHelpTrigger ("--help" );
107+ parser .setGroup (options );
108+ CommandLine cl = parser .parse (args );
109+ CommandLine cmd = cl ;
110+ assertNotNull (cmd );
111+ List b = cmd .getValues ("-b" );
112+ assertEquals ("[1, 2, 10000]" , b + "" );
113+ }
114+ }
0 commit comments