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.docs;
24 ////NOTE: This file contains tags for ASCIIDOC
25 ////DO NOT REMOVE any of those tag lines, e.g.
29 ////DO NOT auto-refresh imports or organise imports!
32 import org.apache.commons.cli.CommandLine;
33 import org.apache.commons.cli.HelpFormatter;
34 import org.junit.Test;
37 import org.onap.policy.apex.tools.common.CliOptions;
38 import org.onap.policy.apex.tools.common.CliParser;
41 import org.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
45 * Examples for documentation using {@link CliParser}.
47 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
49 public class ExampleCliParserTest {
51 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ExampleCliParserTest.class);
54 * Test example parser.
57 public void testExampleParser() {
58 final String[] args = new String[] { "-h" };
61 final String appName = "test-app";
62 final String appDescription = "a test app for documenting how to use the CLI utilities";
66 final CliParser cli = new CliParser();
67 cli.addOption(CliOptions.HELP);
68 cli.addOption(CliOptions.VERSION);
69 cli.addOption(CliOptions.MODELFILE);
73 final CommandLine cmd = cli.parseCli(args);
76 // tag::processCliHelp[]
77 // help is an exit option, print usage and exit
78 if (cmd.hasOption('h') || cmd.hasOption("help")) {
79 final HelpFormatter formatter = new HelpFormatter();
80 LOGGER.info(appName + " v" + cli.getAppVersion() + " - " + appDescription);
81 formatter.printHelp(appName, cli.getOptions());
84 // end::processCliHelp[]
86 // tag::processCliVersion[]
87 // version is an exit option, print version and exit
88 if (cmd.hasOption('v') || cmd.hasOption("version")) {
89 LOGGER.info(appName + " " + cli.getAppVersion());
92 // end::processCliVersion[]
94 // tag::processCliModel[]
95 String modelFile = cmd.getOptionValue('m');
96 if (modelFile == null) {
97 modelFile = cmd.getOptionValue("model");
99 if (modelFile == null) {
100 LOGGER.error(appName + ": no model file given, cannot proceed (try -h for help)");
103 // end::processCliModel[]
105 // tag::someStartPrint[]
106 LOGGER.info(appName + ": starting");
107 LOGGER.info(" --> model file: " + modelFile);
108 // end::someStartPrint[]
111 // your code for the application here
114 // Model2Cli app = new Model2Cli(modelFile, !cmd.hasOption("sv"), appName);
117 // catch(ApexException aex) {
118 // LOGGER.error(appName + ": caught APEX exception with message: " + aex.getMessage());