1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }