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 .cli ;
18+
19+ import java .util .HashSet ;
20+ import java .util .Iterator ;
21+ import java .util .List ;
22+ import java .util .ListIterator ;
23+ import java .util .Set ;
24+
25+ import org .apache .commons .cli2 .Group ;
26+ import org .apache .commons .cli2 .Option ;
27+ import org .apache .commons .cli2 .builder .ArgumentBuilder ;
28+ import org .apache .commons .cli2 .builder .DefaultOptionBuilder ;
29+ import org .apache .commons .cli2 .builder .GroupBuilder ;
30+ import org .apache .commons .cli2 .validation .InvalidArgumentException ;
31+ import org .apache .commons .cli2 .validation .Validator ;
32+
33+ /**
34+ * A utility class for converting data structures version 1 to
35+ * version 2 Option instances.
36+ */
37+ public class CLI2Converter {
38+
39+ private CLI2Converter (){
40+ // prevent creation of static utility class
41+ }
42+
43+ /**
44+ * Creates a version 2 Option instance from a version 1 Option instance.
45+ *
46+ * @param option1 the version 1 Option to convert
47+ * @return a version 2 Option
48+ */
49+ public static Option option (final org .apache .commons .cli .Option option1 ){
50+
51+ final DefaultOptionBuilder obuilder = new DefaultOptionBuilder ();
52+ obuilder .withRequired (option1 .isRequired ());
53+
54+ final String shortName = option1 .getOpt ();
55+ if (shortName !=null && !" " .equals (shortName )){
56+ obuilder .withShortName (shortName );
57+ }
58+
59+ final String longName = option1 .getLongOpt ();
60+ if (longName !=null ){
61+ obuilder .withLongName (longName );
62+ }
63+ obuilder .withId (option1 .getId ());
64+
65+ final String description = option1 .getDescription ();
66+ if (description !=null ){
67+ obuilder .withDescription (description );
68+ }
69+
70+ if (option1 .hasArg ()){
71+ final ArgumentBuilder abuilder = new ArgumentBuilder ();
72+ final String argName = option1 .getArgName ();
73+ abuilder .withName (argName );
74+ abuilder .withMaximum (option1 .getArgs ());
75+ if (option1 .hasValueSeparator ()){
76+ abuilder .withSubsequentSeparator (option1 .getValueSeparator ());
77+ }
78+ if (option1 .hasOptionalArg ()){
79+ abuilder .withMinimum (0 );
80+ }
81+ else {
82+ //TODO check what non-optional arg means
83+ abuilder .withMinimum (option1 .getArgs ());
84+ }
85+
86+ final Object type = option1 .getType ();
87+ if (type !=null ){
88+ abuilder .withValidator (new TypeHandlerValidator (type ));
89+ }
90+
91+ obuilder .withArgument (abuilder .create ());
92+ }
93+
94+ return obuilder .create ();
95+ }
96+
97+ /**
98+ * Creates a version 2 Group instance from a version 1 OptionGroup instance.
99+ *
100+ * @param optionGroup1 the version 1 OptionGroup to convert
101+ * @return a version 2 Group
102+ */
103+ public static Group group (final OptionGroup optionGroup1 ){
104+
105+ final GroupBuilder gbuilder = new GroupBuilder ();
106+
107+ for (final Iterator i = optionGroup1 .getOptions ().iterator ();i .hasNext ();){
108+ final org .apache .commons .cli .Option option1 = (org .apache .commons .cli .Option )i .next ();
109+ final Option option2 = option (option1 );
110+ gbuilder .withOption (option2 );
111+ }
112+
113+ gbuilder .withMaximum (1 );
114+
115+ if (optionGroup1 .isRequired ()){
116+ gbuilder .withMinimum (1 );
117+ }
118+
119+ return gbuilder .create ();
120+ }
121+
122+ /**
123+ * Creates a version 2 Group instance from a version 1 Options instance.
124+ *
125+ * @param options1 the version 1 Options to convert
126+ * @return a version 2 Group
127+ */
128+ public static Group group (final Options options1 ){
129+
130+ final GroupBuilder gbuilder = new GroupBuilder ();
131+
132+ final Set optionGroups = new HashSet ();
133+
134+ for (final Iterator i = options1 .getOptionGroups ().iterator ();i .hasNext ();){
135+ final OptionGroup optionGroup1 = (OptionGroup )i .next ();
136+ Group group = group (optionGroup1 );
137+ gbuilder .withOption (group );
138+ optionGroups .add (optionGroup1 );
139+ }
140+
141+ for (final Iterator i = options1 .getOptions ().iterator ();i .hasNext ();){
142+ final org .apache .commons .cli .Option option1 = (org .apache .commons .cli .Option )i .next ();
143+ if (!optionInAGroup (option1 ,optionGroups )){
144+ final Option option2 = option (option1 );
145+ gbuilder .withOption (option2 );
146+ }
147+ }
148+
149+ return gbuilder .create ();
150+ }
151+
152+ private static boolean optionInAGroup (final org .apache .commons .cli .Option option1 , final Set optionGroups ) {
153+ for (Iterator i = optionGroups .iterator (); i .hasNext ();) {
154+ OptionGroup group = (OptionGroup ) i .next ();
155+ if (group .getOptions ().contains (option1 )){
156+ return true ;
157+ }
158+ }
159+ return false ;
160+ }
161+ }
162+
163+ class TypeHandlerValidator implements Validator {
164+
165+ private final Object type ;
166+
167+ /**
168+ * Creates a new Validator using the TypeHandler class.
169+ *
170+ * @see TypeHandler
171+ * @param type The required type for valid elements
172+ */
173+ public TypeHandlerValidator (final Object type ){
174+ this .type = type ;
175+ }
176+
177+ /* (non-Javadoc)
178+ * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
179+ */
180+ public void validate (final List values ) throws InvalidArgumentException {
181+ final ListIterator i = values .listIterator ();
182+ while (i .hasNext ()){
183+ final String value = (String )i .next ();
184+ final Object converted = TypeHandler .createValue (value ,type );
185+ if (converted ==null ){
186+ throw new InvalidArgumentException ("Unable to understand value: " + value );
187+ }
188+ i .set (converted );
189+ }
190+ }
191+ }
0 commit comments