OSDN Git Service

df78d0fed519ad7c4c3f6f4067e4cca8d93ffcaf
[dvibrowser/dvi2epub.git] / src / jp / sourceforge / dvibrowser / dvi2epub / cmd / AnnotatedCommand.java
1 /*
2  * Copyright 2012 Take-Yuki NAGAO
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *   
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, 
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package jp.sourceforge.dvibrowser.dvi2epub.cmd;
18
19 import java.io.PrintWriter;
20
21 public abstract class AnnotatedCommand extends AbstractCommand {
22         private CommandLineParser parser;
23         private boolean wantHelp;
24         private String[] args;
25
26         public AnnotatedCommand() {
27                 parser = new AnnotatedCommandLineParser(this);
28         }
29
30         @BooleanValueOption(shortName = "h", longName = "help", description = "Show help")
31         public void wantHelp(boolean want) {
32                 this.wantHelp = want;
33         }
34
35         public boolean wantHelp() {
36                 return wantHelp;
37         }
38
39         @Override
40         public int execute(String[] args) throws CommandException {
41                 try {
42                         parser.parse(args);
43                         int ret = doProcessCommandLine();
44                         return ret;
45                 } finally {
46                         PrintWriter out = getWriter();
47                         if (out != null) {
48                                 out.flush();
49                         }
50                 }
51         }
52
53         protected int doProcessCommandLine()
54                         throws CommandException {
55                 if (wantHelp()) {
56                         showUsage();
57                         return Command.EXIT_SUCCESS;
58                 } else {
59                         return processCommandLine();
60                 }
61         }
62
63         protected abstract int processCommandLine() throws CommandException;
64
65         @Override
66         public void showUsage(PrintWriter pw) throws CommandException {
67                 parser.printHelp(pw);
68                 pw.flush();
69         }
70
71         @Override
72         public String getApplicationName() throws CommandException {
73                 return getClass().getName();
74         }
75
76         public String[] getArgs() {
77                 return args;
78         }
79
80         public void setArgs(String[] args) {
81                 this.args = args;
82         }
83         
84         protected OptionMapper createOptionMapper(ParserState state) {
85                 return new DefaultOptionMapper(state);
86         }
87 }