Changes for checkstyle 8.32
[policy/apex-pdp.git] / tools / tools-common / src / main / java / org / onap / policy / apex / tools / common / CliParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.tools.common;
22
23 ////
24 //// NOTE: This file contains tags for ASCIIDOC
25 //// DO NOT REMOVE any of those tag lines, e.g.
26 ////// tag::**
27 ////// end::**
28 ////
29
30 import java.util.Scanner;
31 import org.apache.commons.cli.CommandLine;
32 import org.apache.commons.cli.CommandLineParser;
33 import org.apache.commons.cli.DefaultParser;
34 import org.apache.commons.cli.Option;
35 import org.apache.commons.cli.Options;
36
37 /**
38  * Application CLI parser.
39  *
40  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
41  */
42 public class CliParser {
43
44     /** The local set of CLI options. */
45     private final Options options;
46
47     /** The command line, null on start, not null after successful parse. */
48     private CommandLine cmd;
49
50     /**
51      * Creates a new CLI parser.
52      */
53     public CliParser() {
54         options = new Options();
55     }
56
57     /**
58      * Adds an option to the parser.
59      *
60      * @param option the new option, must not be null
61      * @return self to allow chaining
62      */
63     public CliParser addOption(final Option option) {
64         if (option == null) {
65             throw new IllegalStateException("CLI parser: given option was null");
66         }
67         options.addOption(option);
68         return this;
69     }
70
71     /**
72      * Parses the arguments with the set options.
73      *
74      * @param args the arguments to parse
75      * @return a command line with parsed arguments, null on parse errors.
76      */
77     public CommandLine parseCli(final String[] args) {
78         final CommandLineParser parser = new DefaultParser();
79         try {
80             cmd = parser.parse(options, args);
81         } catch (final Exception ex) {
82             Console.CONSOLE.error("Parsing failed, see reason and cause below");
83             Console.CONSOLE.stacktrace(ex);
84         }
85         return cmd;
86     }
87
88     /**
89      * Returns the parsed command line.
90      *
91      * @return the parsed command line, null if nothing parsed
92      */
93     public CommandLine getCommandLine() {
94         return cmd;
95     }
96
97     /**
98      * Returns the CLI options.
99      *
100      * @return CLI options
101      */
102     public Options getOptions() {
103         return options;
104     }
105
106     /**
107      * Returns the version for an application as set by Maven.
108      *
109      * @return version, null if version file <code>/app-version.txt</code> was not found
110      */
111     @SuppressWarnings("resource")
112     // tag::cliParserVersion[]
113     public String getAppVersion() {
114         return new Scanner(CliParser.class.getResourceAsStream("/app-version.txt"), "UTF-8").useDelimiter("\\A").next();
115     }
116     // end::cliParserVersion[]
117 }