View Javadoc
1   package org.apache.maven.wrapper.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  /**
31   * Parsed command line.
32   */
33  public class ParsedCommandLine
34  {
35      private final Map<String, ParsedCommandLineOption> optionsByString = new HashMap<String, ParsedCommandLineOption>();
36  
37      private final Set<String> presentOptions = new HashSet<String>();
38  
39      private final List<String> extraArguments = new ArrayList<String>();
40  
41      ParsedCommandLine( Iterable<CommandLineOption> options )
42      {
43          for ( CommandLineOption option : options )
44          {
45              ParsedCommandLineOption parsedOption = new ParsedCommandLineOption();
46              for ( String optionStr : option.getOptions() )
47              {
48                  optionsByString.put( optionStr, parsedOption );
49              }
50          }
51      }
52  
53      @Override
54      public String toString()
55      {
56          return String.format( "options: %s, extraArguments: %s", quoteAndJoin( presentOptions ),
57                                quoteAndJoin( extraArguments ) );
58      }
59  
60      private String quoteAndJoin( Iterable<String> strings )
61      {
62          StringBuilder output = new StringBuilder();
63          boolean isFirst = true;
64          for ( String string : strings )
65          {
66              if ( !isFirst )
67              {
68                  output.append( ", " );
69              }
70              output.append( "'" );
71              output.append( string );
72              output.append( "'" );
73              isFirst = false;
74          }
75          return output.toString();
76      }
77  
78      /**
79       * Returns true if the given option is present in this command-line.
80       * 
81       * @param option The option, without the '-' or '--' prefix.
82       * @return true if the option is present.
83       */
84      public boolean hasOption( String option )
85      {
86          option( option );
87          return presentOptions.contains( option );
88      }
89  
90      /**
91       * See also {@link #hasOption}.
92       * 
93       * @param logLevelOptions the options to check
94       * @return true if any of the passed options is present
95       */
96      public boolean hasAnyOption( Collection<String> logLevelOptions )
97      {
98          for ( String option : logLevelOptions )
99          {
100             if ( hasOption( option ) )
101             {
102                 return true;
103             }
104         }
105         return false;
106     }
107 
108     /**
109      * Returns the value of the given option.
110      * 
111      * @param option The option, without the '-' or '--' prefix.
112      * @return The option. never returns null.
113      */
114     public ParsedCommandLineOption option( String option )
115     {
116         ParsedCommandLineOption parsedOption = optionsByString.get( option );
117         if ( parsedOption == null )
118         {
119             throw new IllegalArgumentException( String.format( "Option '%s' not defined.", option ) );
120         }
121         return parsedOption;
122     }
123 
124     public List<String> getExtraArguments()
125     {
126         return extraArguments;
127     }
128 
129     void addExtraValue( String value )
130     {
131         extraArguments.add( value );
132     }
133 
134     ParsedCommandLineOption addOption( String optionStr, CommandLineOption option )
135     {
136         ParsedCommandLineOption parsedOption = optionsByString.get( optionStr );
137         presentOptions.addAll( option.getOptions() );
138         return parsedOption;
139     }
140 }