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.model.generator.model2cli;
23 import java.io.PrintStream;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.onap.policy.apex.tools.common.CliOptions;
29 import org.onap.policy.apex.tools.common.CliParser;
30 import org.onap.policy.apex.tools.common.OutputFile;
33 * Process an Apex Policy Model file to generate the CLI commands to generate an equivalent Apex Policy Model.
35 * @author Sven van der Meer <sven.van.der.meer@ericsson.com>
37 public final class Model2CliMain {
38 /** The name of the application. */
39 public static final String APP_NAME = "gen-model2cli";
41 /** The description 1-liner of the application. */
42 public static final String APP_DESCRIPTION = "generates CLI Editor Commands from a policy model";
47 * @param args the command line arguments
48 * @param outStream for command output
49 * @param errStream for command errors
51 Model2CliMain(final String[] args, final PrintStream outStream, final PrintStream errStream) {
52 final CliParser cli = new CliParser();
53 cli.addOption(CliOptions.HELP);
54 cli.addOption(CliOptions.VERSION);
55 cli.addOption(CliOptions.SKIPVALIDATION);
56 cli.addOption(CliOptions.MODELFILE);
57 cli.addOption(CliOptions.FILEOUT);
58 cli.addOption(CliOptions.OVERWRITE);
60 CommandLine cmd = cli.parseCli(args);
62 // help is an exit option, print usage and exit
63 if (cmd == null || cmd.hasOption(CliOptions.HELP.getOpt())) {
64 outStream.println(getHelpString(cli));
69 // version is an exit option, print version and exit
70 if (cmd.hasOption(CliOptions.VERSION.getOpt())) {
71 outStream.println(APP_NAME + " " + cli.getAppVersion());
75 String modelFile = cmd.getOptionValue(CliOptions.MODELFILE.getOpt());
76 if (modelFile == null) {
77 errStream.println(APP_NAME + ": no '-" + CliOptions.MODELFILE.getOpt()
78 + "' model file given, cannot proceed (try -h for help)");
81 modelFile = cmd.getOptionValue("model");
84 OutputFile outfile = null;
85 final String of = cmd.getOptionValue(CliOptions.FILEOUT.getOpt());
86 final boolean overwrite = cmd.hasOption(CliOptions.OVERWRITE.getOpt());
87 if (overwrite && of == null) {
88 errStream.println(APP_NAME + ": error with '-" + CliOptions.OVERWRITE.getOpt()
89 + "' option. This option is only valid if a '-" + CliOptions.FILEOUT.getOpt()
90 + "' option is also used. Cannot proceed (try -h for help)");
94 outfile = new OutputFile(of, overwrite);
95 final String isoutfileok = outfile.validate();
96 if (isoutfileok != null) {
97 errStream.println(APP_NAME + ": error with '-" + CliOptions.FILEOUT.getOpt() + "' option: \""
98 + isoutfileok + "\". Cannot proceed (try -h for help)");
103 if (outfile == null) {
105 outStream.println(APP_NAME + ": starting CLI generator");
106 outStream.println(" --> model file: " + modelFile);
111 final Model2Cli app = new Model2Cli(modelFile, outfile, !cmd.hasOption("sv"), APP_NAME);
116 * Get the help string for the application.
118 * @param cli the command line options
119 * @return the help string
121 private String getHelpString(final CliParser cli) {
122 HelpFormatter formatter = new HelpFormatter();
124 final StringWriter helpStringWriter = new StringWriter();
125 final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
127 formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
129 return helpStringWriter.toString();
133 * Main method to start the application.
135 * @param args the command line arguments
137 public static void main(final String[] args) {
138 new Model2CliMain(args, System.out, System.err);