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