001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2005-2019 The ConQAT Project                               |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016|                                                                          |
017+-------------------------------------------------------------------------*/
018package org.conqat.lib.commons.options;
019
020import java.io.PrintWriter;
021
022import org.conqat.lib.commons.string.StringUtils;
023
024/**
025 * Base class for running commandline programs.
026 */
027public abstract class CommandLineBase {
028
029        /** Command line parser */
030        private final CommandLine cmdLine = new CommandLine(new OptionRegistry(this));
031
032        /** Help option: print usage and exit. */
033        @Option(shortName = 'h', longName = "help", description = "print this usage message")
034        public void printUsageAndExit() {
035                cmdLine.printUsage(new PrintWriter(System.err));
036                System.exit(1);
037        }
038
039        /** Read the command line. */
040        protected void initFromCommandLine(String[] args) {
041                try {
042                        String[] leftOvers = cmdLine.parse(args);
043                        handleLeftOvers(leftOvers);
044                } catch (OptionException e) {
045                        abort("Incorrect options: " + e.getMessage());
046                }
047        }
048
049        /**
050         * Template methods for dealing with left overs (i.e. parameters that could not
051         * be parsed). The default implementation issues an error if leftovers are
052         * found.
053         */
054        protected void handleLeftOvers(String[] leftOvers) {
055                if (leftOvers.length > 0) {
056                        System.err.println("Unsupported trailing options: " + StringUtils.concat(leftOvers, " "));
057                        printUsageAndExit();
058                }
059        }
060
061        /** Aborts the JVM with the given message. */
062        protected static void abort(String message) {
063                System.err.println(message);
064                System.exit(-1);
065        }
066
067}