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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.tools.common;
24 //// NOTE: This file contains tags for ASCIIDOC
25 //// DO NOT REMOVE any of those tag lines, e.g.
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;
38 * Application CLI parser.
40 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
42 public class CliParser {
44 /** The local set of CLI options. */
45 private final Options options;
47 /** The command line, null on start, not null after successful parse. */
48 private CommandLine cmd;
51 * Creates a new CLI parser.
54 options = new Options();
58 * Adds an option to the parser.
60 * @param option the new option, must not be null
61 * @return self to allow chaining
63 public CliParser addOption(final Option option) {
65 throw new IllegalStateException("CLI parser: given option was null");
67 options.addOption(option);
72 * Parses the arguments with the set options.
74 * @param args the arguments to parse
75 * @return a command line with parsed arguments, null on parse errors.
77 public CommandLine parseCli(final String[] args) {
78 final CommandLineParser parser = new DefaultParser();
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);
89 * Returns the parsed command line.
91 * @return the parsed command line, null if nothing parsed
93 public CommandLine getCommandLine() {
98 * Returns the CLI options.
100 * @return CLI options
102 public Options getOptions() {
107 * Returns the version for an application as set by Maven.
109 * @return version, null if version file <code>/app-version.txt</code> was not found
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();
116 // end::cliParserVersion[]