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