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;
36 import org.onap.policy.apex.tools.common.CliOptions;
37 import org.onap.policy.apex.tools.common.CliParser;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
43 * Examples for documentation using {@link CliParser}.
45 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
47 public class ExampleCliParserTest {
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ExampleCliParserTest.class);
52 * Test example parser.
55 public void testExampleParser() {
56 final String[] args = new String[] { "-h" };
59 final String appName = "test-app";
60 final String appDescription = "a test app for documenting how to use the CLI utilities";
64 final CliParser cli = new CliParser();
65 cli.addOption(CliOptions.HELP);
66 cli.addOption(CliOptions.VERSION);
67 cli.addOption(CliOptions.MODELFILE);
71 final CommandLine cmd = cli.parseCli(args);
74 // tag::processCliHelp[]
75 // help is an exit option, print usage and exit
76 if (cmd.hasOption('h') || cmd.hasOption("help")) {
77 final HelpFormatter formatter = new HelpFormatter();
78 LOGGER.info(appName + " v" + cli.getAppVersion() + " - " + appDescription);
79 formatter.printHelp(appName, cli.getOptions());
82 // end::processCliHelp[]
84 // tag::processCliVersion[]
85 // version is an exit option, print version and exit
86 if (cmd.hasOption('v') || cmd.hasOption("version")) {
87 LOGGER.info(appName + " " + cli.getAppVersion());
90 // end::processCliVersion[]
92 // tag::processCliModel[]
93 String modelFile = cmd.getOptionValue('m');
94 if (modelFile == null) {
95 modelFile = cmd.getOptionValue("model");
97 if (modelFile == null) {
98 LOGGER.error(appName + ": no model file given, cannot proceed (try -h for help)");
101 // end::processCliModel[]
103 // tag::someStartPrint[]
104 LOGGER.info(appName + ": starting");
105 LOGGER.info(" --> model file: " + modelFile);
106 // end::someStartPrint[]
109 // your code for the application here
112 // Model2Cli app = new Model2Cli(modelFile, !cmd.hasOption("sv"), appName);
115 // catch(ApexException aex) {
116 // LOGGER.error(appName + ": caught APEX exception with message: " + aex.getMessage());