Sync Integ to Master
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / cli / CLITool.java
1 package org.openecomp.sdc.asdctool.cli;
2
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;
11
12 /**
13  * abstract base class to extend when implementing a cli tool
14  */
15 public abstract class CLITool {
16
17     private static final Logger LOGGER = LoggerFactory.getLogger(CLITool.class);
18
19     public CLIToolData init(String[] args) {
20         CommandLine commandLine = initCmdLineOptions(args);
21         return new CLIToolData(commandLine);
22     }
23
24     private CommandLine initCmdLineOptions(String[] args) {
25         Options options = buildCmdLineOptions();
26         CommandLineParser parser = new DefaultParser();
27         try {
28             return parser.parse( options, args );
29         }
30         catch( ParseException exp ) {
31             LOGGER.error("Parsing failed.  Reason: " + exp.getMessage() );
32             usageAndExit(options);
33             return null;
34         }
35     }
36
37     private void usageAndExit(Options options) {
38         HelpFormatter formatter = new HelpFormatter();
39         formatter.printHelp(commandName(), options );
40         System.exit(1);
41     }
42
43     /**
44      *
45      * @return all command line options required by this command line tool
46      */
47     protected abstract Options buildCmdLineOptions();
48
49     /**
50      *
51      * @return the command name
52      */
53     protected abstract String commandName();
54
55
56 }