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;
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;
39 * Application CLI parser.
41 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
43 public class CliParser {
45 /** The local set of CLI options. */
46 private final Options options;
48 /** The command line, null on start, not null after successful parse. */
49 private CommandLine cmd;
52 * Creates a new CLI parser.
55 options = new Options();
59 * Adds an option to the parser.
61 * @param option the new option, must not be null
62 * @return self to allow chaining
64 public CliParser addOption(final Option option) {
66 throw new IllegalStateException("CLI parser: given option was null");
68 options.addOption(option);
73 * Parses the arguments with the set options.
75 * @param args the arguments to parse
76 * @return a command line with parsed arguments, null on parse errors.
78 public CommandLine parseCli(final String[] args) {
79 final CommandLineParser parser = new DefaultParser();
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);
90 * Returns the parsed command line.
92 * @return the parsed command line, null if nothing parsed
94 public CommandLine getCommandLine() {
99 * Returns the CLI options.
101 * @return CLI options
103 public Options getOptions() {
108 * Returns the version for an application as set by Maven.
110 * @return version, null if version file <code>/app-version.txt</code> was not found
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();
117 // end::cliParserVersion[]