KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > main > ColumbaCmdLineParser


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.main;
19
20 import org.apache.commons.cli.BasicParser;
21 import org.apache.commons.cli.CommandLine;
22 import org.apache.commons.cli.CommandLineParser;
23 import org.apache.commons.cli.HelpFormatter;
24 import org.apache.commons.cli.Option;
25 import org.apache.commons.cli.OptionGroup;
26 import org.apache.commons.cli.Options;
27 import org.apache.commons.cli.ParseException;
28 import org.columba.core.resourceloader.GlobalResourceLoader;
29
30 /**
31  * Parsing the commandline arguments and setting states, that can be used from
32  * other components.
33  *
34  * @author waffel
35  */

36 public class ColumbaCmdLineParser {
37
38     private static final String JavaDoc RESOURCE_PATH = "org.columba.core.i18n.global";
39
40     private CommandLineParser parser;
41
42     private Options options;
43
44     private String JavaDoc[] args;
45
46     private static ColumbaCmdLineParser instance;
47
48     private CommandLine commandLine;
49
50     // switch for restoring last session of Columba.
51
// if true, restores all windows.
52
private boolean restoreLastSession = true;
53
54     private ColumbaCmdLineParser() {
55         parser = new BasicParser();
56         options = new Options();
57     }
58
59     /**
60      * Gets the instance of the ColumbaCmdLineParser.
61      *
62      * @return the singleton instance
63      */

64     public static ColumbaCmdLineParser getInstance() {
65         if (instance == null) {
66             instance = new ColumbaCmdLineParser();
67         }
68
69         return instance;
70     }
71
72     /**
73      * Adds an option to the CommandlineParser
74      *
75      * @param option
76      * a new command line argument.
77      */

78     public void addOption(Option option) {
79         options.addOption(option);
80     }
81
82     /**
83      * Adds an OptionGroup to the CommandlineParser.
84      *
85      * @param option
86      */

87     public void addOptionGroup(OptionGroup option) {
88         options.addOptionGroup(option);
89     }
90
91     /**
92      * Parses the commandline.
93      *
94      * @param args
95      * the arguments
96      * @return the parsed CommandLine
97      * @throws ParseException
98      */

99     public CommandLine parse(String JavaDoc[] args) throws ParseException {
100         commandLine = parser.parse(options, args);
101
102         return commandLine;
103     }
104
105     /**
106      * Gets the previously parsed Commandline.
107      *
108      * @see #parse(String[])
109      *
110      * @return the last parsed commandline
111      */

112     public CommandLine getParsedCommandLine() {
113         return commandLine;
114     }
115
116     /**
117      * prints the usage of the program with commandline arguments.
118      */

119     public void printUsage() {
120         // automatically generate the help statement
121
HelpFormatter formatter = new HelpFormatter();
122         formatter.printHelp(GlobalResourceLoader.getString(RESOURCE_PATH,
123                 "global", "cmdline_usage"), options);
124     }
125
126     /**
127      * @return Returns the args.
128      */

129     public String JavaDoc[] getArgs() {
130         return args;
131     }
132
133     /**
134      * @param restoreLastSession
135      * The restoreLastSession to set.
136      */

137     public void setRestoreLastSession(boolean restoreLastSession) {
138         this.restoreLastSession = restoreLastSession;
139     }
140
141     public boolean getRestoreLastSession() {
142         return this.restoreLastSession;
143     }
144 }
Popular Tags