1 package org.openecomp.sdc.asdctool.cli;
3 import org.apache.commons.cli.CommandLine;
4 import org.apache.commons.cli.CommandLineParser;
5 import org.apache.commons.cli.DefaultParser;
6 import org.apache.commons.cli.HelpFormatter;
7 import org.apache.commons.cli.Options;
8 import org.apache.commons.cli.ParseException;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
13 * abstract base class to extend when implementing a cli tool
15 public abstract class CLITool {
17 private static final Logger LOGGER = LoggerFactory.getLogger(CLITool.class);
19 public CLIToolData init(String[] args) {
20 CommandLine commandLine = initCmdLineOptions(args);
21 return new CLIToolData(commandLine);
24 private CommandLine initCmdLineOptions(String[] args) {
25 Options options = buildCmdLineOptions();
26 CommandLineParser parser = new DefaultParser();
28 return parser.parse( options, args );
30 catch( ParseException exp ) {
31 LOGGER.error("Parsing failed. Reason: " + exp.getMessage() );
32 usageAndExit(options);
37 private void usageAndExit(Options options) {
38 HelpFormatter formatter = new HelpFormatter();
39 formatter.printHelp(commandName(), options );
45 * @return all command line options required by this command line tool
47 protected abstract Options buildCmdLineOptions();
51 * @return the command name
53 protected abstract String commandName();