2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.gui.editors.apex.rest;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.util.Arrays;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.DefaultParser;
30 import org.apache.commons.cli.HelpFormatter;
31 import org.apache.commons.cli.Option;
32 import org.apache.commons.cli.Options;
33 import org.apache.commons.cli.ParseException;
36 * This class reads and handles command line parameters to the Apex CLI editor.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class ApexEditorParameterParser {
41 // Apache Commons CLI options
42 private Options options;
44 private static final int COMMAND_HELP_MAX_LINE_WIDTH = 120;
47 * Construct the options for the CLI editor.
49 public ApexEditorParameterParser() {
51 options = new Options();
52 options.addOption("h", "help", false, "outputs the usage of this command");
57 .desc("port to use for the Apex RESTful editor REST calls.")
67 .longOpt("time-to-live")
68 .desc("the amount of time in seconds that the server will run for before terminating. "
69 + "Default value is " + ApexEditorParameters.INFINITY_TIME_TO_LIVE + " to run indefinitely.")
71 .argName("TIME_TO_LIVE")
80 .desc("the IP address to listen on. Default value is " + ApexEditorParameters.DEFAULT_SERVER_URI_ROOT
81 + " to restrict access to the local machine only.")
91 .longOpt("upload-userid")
92 .desc("the userid to use for uploads. Default value is null. Must be specified if the upload-url "
93 + "parameter is specified")
94 .hasArg().argName("USERID")
102 .longOpt("upload-url")
103 .desc("the URL to use for uploads. Default value is null")
105 .argName("UPLOAD_URL")
114 * Parse the command line options.
116 * @param args The arguments
117 * @return the apex editor parameters
119 public ApexEditorParameters parse(final String[] args) {
120 CommandLine commandLine = null;
122 commandLine = new DefaultParser().parse(options, args);
123 } catch (final ParseException e) {
124 throw new ApexEditorParameterException("invalid command line arguments specified : " + e.getMessage());
127 final var parameters = new ApexEditorParameters();
128 final String[] remainingArgs = commandLine.getArgs();
130 if (commandLine.getArgs().length > 0) {
131 throw new ApexEditorParameterException(
132 "too many command line arguments specified : " + Arrays.toString(remainingArgs));
135 if (commandLine.hasOption('h')) {
136 parameters.setHelp(true);
139 if (commandLine.hasOption('p')) {
140 parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
142 } catch (final ParseException e) {
143 throw new ApexEditorParameterException("error parsing argument \"port\" :" + e.getMessage(), e);
146 if (commandLine.hasOption('t')) {
147 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
149 } catch (final ParseException e) {
150 throw new ApexEditorParameterException("error parsing argument \"time-to-live\" :" + e.getMessage(), e);
153 if (commandLine.hasOption('l')) {
154 parameters.setListenAddress(commandLine.getParsedOptionValue("listen").toString());
156 } catch (final ParseException e) {
157 throw new ApexEditorParameterException("error parsing argument \"listen-address\" :" + e.getMessage(), e);
160 if (commandLine.hasOption("uuid")) {
161 parameters.setUploadUserid(commandLine.getParsedOptionValue("uuid").toString());
163 } catch (final ParseException e) {
164 throw new ApexEditorParameterException("error parsing argument \"upload-uuid\" :" + e.getMessage(), e);
167 if (commandLine.hasOption("uurl")) {
168 parameters.setUploadUrl(commandLine.getParsedOptionValue("uurl").toString());
170 } catch (final ParseException e) {
171 throw new ApexEditorParameterException("error parsing argument \"upload-url\" :" + e.getMessage(), e);
178 * Get help information.
180 * @param mainClassName the main class name
183 public String getHelp(final String mainClassName) {
184 final var stringWriter = new StringWriter();
185 final var stringPrintWriter = new PrintWriter(stringWriter);
187 final var helpFormatter = new HelpFormatter();
188 helpFormatter.printHelp(stringPrintWriter, COMMAND_HELP_MAX_LINE_WIDTH, mainClassName + " [options...] ", null,
191 return stringWriter.toString();