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.Collections;
22  import java.util.List;
23  
24  import org.apache.commons.cli.ParseException;
25  import org.apache.maven.api.cli.Options;
26  import org.apache.maven.api.cli.mvnsh.ShellOptions;
27  import org.apache.maven.cling.invoker.BaseParser;
28  
29  public class ShellParser extends BaseParser {
30      @Override
31      protected ShellOptions emptyOptions() {
32          try {
33              return CommonsCliShellOptions.parse(new String[0]);
34          } catch (ParseException e) {
35              throw new IllegalArgumentException(e);
36          }
37      }
38  
39      @Override
40      protected ShellInvokerRequest getInvokerRequest(LocalContext context) {
41          return new ShellInvokerRequest(
42                  context.parserRequest,
43                  context.parsingFailed,
44                  context.cwd,
45                  context.installationDirectory,
46                  context.userHomeDirectory,
47                  context.userProperties,
48                  context.systemProperties,
49                  context.topDirectory,
50                  context.rootDirectory,
51                  context.extensions,
52                  (ShellOptions) context.options);
53      }
54  
55      @Override
56      protected List<Options> parseCliOptions(LocalContext context) {
57          return Collections.singletonList(parseShellCliOptions(context.parserRequest.args()));
58      }
59  
60      protected CommonsCliShellOptions parseShellCliOptions(List<String> args) {
61          try {
62              return CommonsCliShellOptions.parse(args.toArray(new String[0]));
63          } catch (ParseException e) {
64              throw new IllegalArgumentException("Failed to parse command line options: " + e.getMessage(), e);
65          }
66      }
67  
68      @Override
69      protected Options assembleOptions(List<Options> parsedOptions) {
70          // nothing to assemble, we deal with CLI only
71          return parsedOptions.get(0);
72      }
73  }