1 package org.openecomp.sdc.asdctool.cli;
3 import org.apache.commons.cli.*;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
8 * abstract base class to extend when implementing a cli tool
10 public abstract class CLITool {
12 private static final Logger LOGGER = LoggerFactory.getLogger(CLITool.class);
14 public CLIToolData init(String[] args) {
15 CommandLine commandLine = initCmdLineOptions(args);
16 return new CLIToolData(commandLine);
19 private CommandLine initCmdLineOptions(String[] args) {
20 Options options = buildCmdLineOptions();
21 CommandLineParser parser = new DefaultParser();
23 return parser.parse( options, args );
25 catch( ParseException exp ) {
26 LOGGER.error("Parsing failed. Reason: " + exp.getMessage() );
27 usageAndExit(options);
32 private void usageAndExit(Options options) {
33 HelpFormatter formatter = new HelpFormatter();
34 formatter.printHelp(commandName(), options );
40 * @return all command line options required by this command line tool
42 protected abstract Options buildCmdLineOptions();
46 * @return the command name
48 protected abstract String commandName();