dfaafbd25c9ee10ac43c4738f798dbb8713c4a66
[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
32 import org.apache.commons.cli.CommandLine;
33 import org.apache.commons.cli.CommandLineParser;
34 import org.apache.commons.cli.DefaultParser;
35 import org.apache.commons.cli.Option;
36 import org.apache.commons.cli.Options;
37
38 /**
39  * Application CLI parser.
40  *
41  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
42  */
43 public class CliParser {
44
45     /** The local set of CLI options. */
46     private final Options options;
47
48     /** The command line, null on start, not null after successful parse. */
49     private CommandLine cmd;
50
51     /**
52      * Creates a new CLI parser.
53      */
54     public CliParser() {
55         options = new Options();
56     }
57
58     /**
59      * Adds an option to the parser.
60      *
61      * @param option the new option, must not be null
62      * @return self to allow chaining
63      */
64     public CliParser addOption(final Option option) {
65         if (option == null) {
66             throw new IllegalStateException("CLI parser: given option was null");
67         }
68         options.addOption(option);
69         return this;
70     }
71
72     /**
73      * Parses the arguments with the set options.
74      *
75      * @param args the arguments to parse
76      * @return a command line with parsed arguments, null on parse errors.
77      */
78     public CommandLine parseCli(final String[] args) {
79         final CommandLineParser parser = new DefaultParser();
80         try {
81             cmd = parser.parse(options, args);
82         } catch (final Exception ex) {
83             Console.CONSOLE.error("Parsing failed, see reason and cause below");
84             Console.CONSOLE.stacktrace(ex);
85         }
86         return cmd;
87     }
88
89     /**
90      * Returns the parsed command line.
91      *
92      * @return the parsed command line, null if nothing parsed
93      */
94     public CommandLine getCommandLine() {
95         return cmd;
96     }
97
98     /**
99      * Returns the CLI options.
100      *
101      * @return CLI options
102      */
103     public Options getOptions() {
104         return options;
105     }
106
107     /**
108      * Returns the version for an application as set by Maven.
109      *
110      * @return version, null if version file <code>/app-version.txt</code> was not found
111      */
112     @SuppressWarnings("resource")
113     // tag::cliParserVersion[]
114     public String getAppVersion() {
115         return new Scanner(CliParser.class.getResourceAsStream("/app-version.txt"), "UTF-8").useDelimiter("\\A").next();
116     }
117     // end::cliParserVersion[]
118 }