View Javadoc
1   /*
2     Copyright (C) 2020 - 2022 Alexander Kapitman
3   
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15  */
16  
17  package ru.akman.maven.plugins;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  import java.util.stream.Collectors;
23  import org.codehaus.plexus.util.cli.Arg;
24  import org.codehaus.plexus.util.cli.Commandline;
25  
26  /**
27   * Command line option (group of arguments).
28   */
29  public class CommandLineOption {
30  
31    /**
32     * Command line.
33     */
34    private final Commandline cmdLine;
35  
36    /**
37     * List of the option arguments.
38     */
39    private final List<Arg> args;
40  
41    /**
42     * Create an option.
43     *
44     * @param cmdLine command line
45     */
46    public CommandLineOption(final Commandline cmdLine) {
47      this.cmdLine = cmdLine;
48      this.args = new ArrayList<>();
49    }
50  
51    /**
52     * Create argument as part of the option.
53     *
54     * @return the created argument
55     */
56    public Arg createArg() {
57      final Arg arg = cmdLine.createArg();
58      args.add(arg);
59      return arg;
60    }
61  
62    /**
63     * Create the option string representation as arguments joined with space.
64     *
65     * @return the created string representation
66     */
67    @Override
68    public String toString() {
69      return args.stream()
70        .flatMap(arg -> Arrays.stream(arg.getParts()))
71        .collect(Collectors.joining(" "));
72    }
73  
74  }