001/* 
002 * Copyright (C) 2016 Hobrasoft s.r.o.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Affero General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Affero General Public License for more details.
013 *
014 * You should have received a copy of the GNU Affero General Public License
015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
016 */
017package cz.hobrasoft.pdfmu;
018
019import java.util.Map;
020import net.sourceforge.argparse4j.inf.Argument;
021import net.sourceforge.argparse4j.inf.ArgumentAction;
022import net.sourceforge.argparse4j.inf.ArgumentParser;
023
024/**
025 * Argument action to print a message and exit program. Inspired by
026 * {@link net.sourceforge.argparse4j.impl.action.VersionArgumentAction}.
027 *
028 * @author Filip Bartek
029 */
030public class PrintAndExitAction implements ArgumentAction {
031
032    private final String message;
033
034    public PrintAndExitAction(String message) {
035        this.message = message;
036    }
037
038    /**
039     * Prints the message to {@link System#out} and terminates the program with
040     * the exit code 0. All the parameters are ignored.
041     */
042    @Override
043    public void run(ArgumentParser parser, Argument arg,
044            Map<String, Object> attrs, String flag, Object value) {
045        System.out.print(message);
046        System.exit(0);
047    }
048
049    @Override
050    public void onAttach(Argument arg) {
051        // Do nothing
052    }
053
054    /**
055     * @return always false because this action does not consume and argument
056     */
057    @Override
058    public boolean consumeArgument() {
059        return false;
060    }
061}