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;
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.HelpFormatter;
29 import org.onap.policy.apex.tools.common.CliOptions;
30 import org.onap.policy.apex.tools.common.CliParser;
31 import org.onap.policy.apex.tools.common.OutputFile;
34 * Process an Apex Policy Model file to generate the CLI commands to generate an equivalent Apex Policy Model.
36 * @author Sven van der Meer <sven.van.der.meer@ericsson.com>
38 public final class Model2CliMain {
39 /** The name of the application. */
40 public static final String APP_NAME = "gen-model2cli";
42 /** The description 1-liner of the application. */
43 public static final String APP_DESCRIPTION = "generates CLI Editor Commands from a policy model";
48 * @param args the command line arguments
49 * @param outStream for command output
50 * @param errStream for command errors
52 Model2CliMain(final String[] args, final PrintStream outStream, final PrintStream errStream) {
53 final CliParser cli = new CliParser();
54 cli.addOption(CliOptions.HELP);
55 cli.addOption(CliOptions.VERSION);
56 cli.addOption(CliOptions.SKIPVALIDATION);
57 cli.addOption(CliOptions.MODELFILE);
58 cli.addOption(CliOptions.FILEOUT);
59 cli.addOption(CliOptions.OVERWRITE);
61 CommandLine cmd = cli.parseCli(args);
63 // help is an exit option, print usage and exit
64 if (cmd == null || cmd.hasOption(CliOptions.HELP.getOpt())) {
65 outStream.println(getHelpString(cli));
70 // version is an exit option, print version and exit
71 if (cmd.hasOption(CliOptions.VERSION.getOpt())) {
72 outStream.println(APP_NAME + " " + cli.getAppVersion());
76 String modelFile = cmd.getOptionValue(CliOptions.MODELFILE.getOpt());
77 if (modelFile == null) {
78 errStream.println(APP_NAME + ": no '-" + CliOptions.MODELFILE.getOpt()
79 + "' model file given, cannot proceed (try -h for help)");
82 modelFile = cmd.getOptionValue("model");
85 OutputFile outfile = null;
86 final String of = cmd.getOptionValue(CliOptions.FILEOUT.getOpt());
87 final boolean overwrite = cmd.hasOption(CliOptions.OVERWRITE.getOpt());
88 if (overwrite && of == null) {
89 errStream.println(APP_NAME + ": error with '-" + CliOptions.OVERWRITE.getOpt()
90 + "' option. This option is only valid if a '-" + CliOptions.FILEOUT.getOpt()
91 + "' option is also used. Cannot proceed (try -h for help)");
95 outfile = new OutputFile(of, overwrite);
96 final String isoutfileok = outfile.validate();
97 if (isoutfileok != null) {
98 errStream.println(APP_NAME + ": error with '-" + CliOptions.FILEOUT.getOpt() + "' option: \""
99 + isoutfileok + "\". Cannot proceed (try -h for help)");
104 if (outfile == null) {
106 outStream.println(APP_NAME + ": starting CLI generator");
107 outStream.println(" --> model file: " + modelFile);
112 final Model2Cli app = new Model2Cli(modelFile, outfile, !cmd.hasOption("sv"), APP_NAME);
117 * Get the help string for the application.
119 * @param cli the command line options
120 * @return the help string
122 private String getHelpString(final CliParser cli) {
123 HelpFormatter formatter = new HelpFormatter();
125 final StringWriter helpStringWriter = new StringWriter();
126 final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
128 formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
130 return helpStringWriter.toString();
134 * Main method to start the application.
136 * @param args the command line arguments
138 public static void main(final String[] args) {
139 new Model2CliMain(args, System.out, System.err);