2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.gui.editors.apex.rest;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.Arrays;
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.DefaultParser;
29 import org.apache.commons.cli.HelpFormatter;
30 import org.apache.commons.cli.Option;
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.ParseException;
35 * This class reads and handles command line parameters to the Apex CLI editor.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class ApexEditorParameterParser {
40 // Apache Commons CLI options
41 private Options options;
43 private static final int COMMAND_HELP_MAX_LINE_WIDTH = 120;
46 * Construct the options for the CLI editor.
48 public ApexEditorParameterParser() {
50 options = new Options();
51 options.addOption("h", "help", false, "outputs the usage of this command");
56 .desc("port to use for the Apex RESTful editor REST calls.")
66 .longOpt("time-to-live")
67 .desc("the amount of time in seconds that the server will run for before terminating. "
68 + "Default value is " + ApexEditorParameters.INFINITY_TIME_TO_LIVE + " to run indefinitely.")
70 .argName("TIME_TO_LIVE")
79 .desc("the IP address to listen on. Default value is " + ApexEditorParameters.DEFAULT_SERVER_URI_ROOT
80 + " to restrict access to the local machine only.")
90 .longOpt("upload-userid")
91 .desc("the userid to use for uploads. Default value is null. Must be specified if the upload-url "
92 + "parameter is specified")
93 .hasArg().argName("USERID")
101 .longOpt("upload-url")
102 .desc("the URL to use for uploads. Default value is null")
113 * Parse the command line options.
115 * @param args The arguments
116 * @return the apex editor parameters
118 public ApexEditorParameters parse(final String[] args) {
119 CommandLine commandLine = null;
121 commandLine = new DefaultParser().parse(options, args);
122 } catch (final ParseException e) {
123 throw new ApexEditorParameterException("invalid command line arguments specified : " + e.getMessage());
126 final ApexEditorParameters parameters = new ApexEditorParameters();
127 final String[] remainingArgs = commandLine.getArgs();
129 if (commandLine.getArgs().length > 0) {
130 throw new ApexEditorParameterException(
131 "too many command line arguments specified : " + Arrays.toString(remainingArgs));
134 if (commandLine.hasOption('h')) {
135 parameters.setHelp(true);
138 if (commandLine.hasOption('p')) {
139 parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
141 } catch (final ParseException e) {
142 throw new ApexEditorParameterException("error parsing argument \"port\" :" + e.getMessage(), e);
145 if (commandLine.hasOption('t')) {
146 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
148 } catch (final ParseException e) {
149 throw new ApexEditorParameterException("error parsing argument \"time-to-live\" :" + e.getMessage(), e);
152 if (commandLine.hasOption('l')) {
153 parameters.setListenAddress(commandLine.getParsedOptionValue("listen").toString());
155 } catch (final ParseException e) {
156 throw new ApexEditorParameterException("error parsing argument \"listen-address\" :" + e.getMessage(), e);
159 if (commandLine.hasOption("uuid")) {
160 parameters.setUploadUserid(commandLine.getParsedOptionValue("uuid").toString());
162 } catch (final ParseException e) {
163 throw new ApexEditorParameterException("error parsing argument \"upload-uuid\" :" + e.getMessage(), e);
166 if (commandLine.hasOption("uurl")) {
167 parameters.setUploadUrl(commandLine.getParsedOptionValue("uurl").toString());
169 } catch (final ParseException e) {
170 throw new ApexEditorParameterException("error parsing argument \"upload-url\" :" + e.getMessage(), e);
177 * Get help information.
179 * @param mainClassName the main class name
182 public String getHelp(final String mainClassName) {
183 final StringWriter stringWriter = new StringWriter();
184 final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
186 final HelpFormatter helpFormatter = new HelpFormatter();
187 helpFormatter.printHelp(stringPrintWriter, COMMAND_HELP_MAX_LINE_WIDTH, mainClassName + " [options...] ", null,
190 return stringWriter.toString();