001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.main;
019:
020: import org.apache.commons.cli.BasicParser;
021: import org.apache.commons.cli.CommandLine;
022: import org.apache.commons.cli.CommandLineParser;
023: import org.apache.commons.cli.HelpFormatter;
024: import org.apache.commons.cli.Option;
025: import org.apache.commons.cli.OptionGroup;
026: import org.apache.commons.cli.Options;
027: import org.apache.commons.cli.ParseException;
028: import org.columba.core.resourceloader.GlobalResourceLoader;
029:
030: /**
031: * Parsing the commandline arguments and setting states, that can be used from
032: * other components.
033: *
034: * @author waffel
035: */
036: public class ColumbaCmdLineParser {
037:
038: private static final String RESOURCE_PATH = "org.columba.core.i18n.global";
039:
040: private CommandLineParser parser;
041:
042: private Options options;
043:
044: private String[] args;
045:
046: private static ColumbaCmdLineParser instance;
047:
048: private CommandLine commandLine;
049:
050: // switch for restoring last session of Columba.
051: // if true, restores all windows.
052: private boolean restoreLastSession = true;
053:
054: private ColumbaCmdLineParser() {
055: parser = new BasicParser();
056: options = new Options();
057: }
058:
059: /**
060: * Gets the instance of the ColumbaCmdLineParser.
061: *
062: * @return the singleton instance
063: */
064: public static ColumbaCmdLineParser getInstance() {
065: if (instance == null) {
066: instance = new ColumbaCmdLineParser();
067: }
068:
069: return instance;
070: }
071:
072: /**
073: * Adds an option to the CommandlineParser
074: *
075: * @param option
076: * a new command line argument.
077: */
078: public void addOption(Option option) {
079: options.addOption(option);
080: }
081:
082: /**
083: * Adds an OptionGroup to the CommandlineParser.
084: *
085: * @param option
086: */
087: public void addOptionGroup(OptionGroup option) {
088: options.addOptionGroup(option);
089: }
090:
091: /**
092: * Parses the commandline.
093: *
094: * @param args
095: * the arguments
096: * @return the parsed CommandLine
097: * @throws ParseException
098: */
099: public CommandLine parse(String[] 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(
123: RESOURCE_PATH, "global", "cmdline_usage"), options);
124: }
125:
126: /**
127: * @return Returns the args.
128: */
129: public String[] 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: }
|