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.cling.invoker.mvnsh;
20  
21  import java.util.List;
22  import java.util.ListIterator;
23  import java.util.function.UnaryOperator;
24  
25  import org.apache.commons.cli.CommandLine;
26  import org.apache.commons.cli.Option;
27  import org.apache.commons.cli.ParseException;
28  import org.apache.maven.api.cli.Options;
29  import org.apache.maven.api.cli.mvnsh.ShellOptions;
30  import org.apache.maven.api.services.Interpolator;
31  import org.apache.maven.api.services.InterpolatorException;
32  import org.apache.maven.cling.invoker.CommonsCliOptions;
33  
34  import static org.apache.maven.cling.invoker.Utils.createInterpolator;
35  
36  /**
37   * Implementation of {@link ShellOptions} (base + shell).
38   */
39  public class CommonsCliShellOptions extends CommonsCliOptions implements ShellOptions {
40      public static CommonsCliShellOptions parse(String[] args) throws ParseException {
41          CLIManager cliManager = new CLIManager();
42          return new CommonsCliShellOptions(Options.SOURCE_CLI, cliManager, cliManager.parse(args));
43      }
44  
45      protected CommonsCliShellOptions(String source, CLIManager cliManager, CommandLine commandLine) {
46          super(source, cliManager, commandLine);
47      }
48  
49      private static CommonsCliShellOptions interpolate(CommonsCliShellOptions options, UnaryOperator<String> callback) {
50          try {
51              // now that we have properties, interpolate all arguments
52              Interpolator interpolator = createInterpolator();
53              CommandLine.Builder commandLineBuilder = new CommandLine.Builder();
54              commandLineBuilder.setDeprecatedHandler(o -> {});
55              for (Option option : options.commandLine.getOptions()) {
56                  if (!CLIManager.USER_PROPERTY.equals(option.getOpt())) {
57                      List<String> values = option.getValuesList();
58                      for (ListIterator<String> it = values.listIterator(); it.hasNext(); ) {
59                          it.set(interpolator.interpolate(it.next(), callback));
60                      }
61                  }
62                  commandLineBuilder.addOption(option);
63              }
64              for (String arg : options.commandLine.getArgList()) {
65                  commandLineBuilder.addArg(interpolator.interpolate(arg, callback));
66              }
67              return new CommonsCliShellOptions(
68                      options.source, (CLIManager) options.cliManager, commandLineBuilder.build());
69          } catch (InterpolatorException e) {
70              throw new IllegalArgumentException("Could not interpolate CommonsCliOptions", e);
71          }
72      }
73  
74      @Override
75      public ShellOptions interpolate(UnaryOperator<String> callback) {
76          return interpolate(this, callback);
77      }
78  
79      protected static class CLIManager extends CommonsCliOptions.CLIManager {
80          @Override
81          protected String commandLineSyntax(String command) {
82              return command + " [options]";
83          }
84      }
85  }