View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu;
18  
19  import java.util.Map;
20  import net.sourceforge.argparse4j.inf.Argument;
21  import net.sourceforge.argparse4j.inf.ArgumentAction;
22  import net.sourceforge.argparse4j.inf.ArgumentParser;
23  
24  /**
25   * Argument action to print a message and exit program. Inspired by
26   * {@link net.sourceforge.argparse4j.impl.action.VersionArgumentAction}.
27   *
28   * @author Filip Bartek
29   */
30  public class PrintAndExitAction implements ArgumentAction {
31  
32      private final String message;
33  
34      public PrintAndExitAction(String message) {
35          this.message = message;
36      }
37  
38      /**
39       * Prints the message to {@link System#out} and terminates the program with
40       * the exit code 0. All the parameters are ignored.
41       */
42      @Override
43      public void run(ArgumentParser parser, Argument arg,
44              Map<String, Object> attrs, String flag, Object value) {
45          System.out.print(message);
46          System.exit(0);
47      }
48  
49      @Override
50      public void onAttach(Argument arg) {
51          // Do nothing
52      }
53  
54      /**
55       * @return always false because this action does not consume and argument
56       */
57      @Override
58      public boolean consumeArgument() {
59          return false;
60      }
61  }