home *** CD-ROM | disk | FTP | other *** search
- package com.commerceone.util.cmdline;
-
- import com.commerceone.util.contract.Contract;
- import java.util.Enumeration;
- import java.util.Locale;
- import java.util.MissingResourceException;
- import java.util.ResourceBundle;
-
- public abstract class CmdLineApp implements CmdLineAppConstants {
- private ArgumentParser argumentParser = new ArgumentParser(this);
- private String appName;
- protected String remainingMeaning;
- protected String remainingExplainSuccinct;
- protected String remainingExplainVerbose;
- private ResourceBundle displayBundle;
-
- protected void registerOption(String opt, String succinctExplain, String verboseExplain, boolean isBoolean) {
- this.argumentParser.registerOption(opt, succinctExplain, verboseExplain, isBoolean);
- }
-
- protected void registerOption(String opt, String succinctExplain, String verboseExplain, boolean isBoolean, boolean shouldDisplay) {
- this.argumentParser.registerOption(opt, succinctExplain, verboseExplain, isBoolean, shouldDisplay);
- }
-
- public String usageString() {
- String useString = this.displayBundle.getString("usage") + this.getAppName() + " ";
- Enumeration enumKeys = this.argumentParser.getRegisteredValues().elements();
-
- while(enumKeys.hasMoreElements()) {
- Object object = enumKeys.nextElement();
- if (object instanceof RegisteredOption) {
- RegisteredOption option = (RegisteredOption)object;
- if (option.getShouldDisplay()) {
- useString = useString + "[-" + option.getOption() + " " + option.getSuccinctExplanation() + "] ";
- }
- }
- }
-
- if (this.getRemainingMeaning() != null && this.getRemainingSuccinct() != null) {
- useString = useString + "[" + this.getRemainingMeaning() + " " + this.getRemainingSuccinct() + "]\"";
- } else {
- useString = useString + "\"";
- }
-
- return useString;
- }
-
- public CmdLineApp(String name) {
- this.appName = name;
-
- try {
- this.displayBundle = ResourceBundle.getBundle("com.commerceone.util.cmdline.CmdLineAppBundle", Locale.getDefault());
- this.displayBundle.getString("usage");
- this.displayBundle.getString("option");
- } catch (MissingResourceException var3) {
- Contract.require(false, "Should not happen");
- this.displayBundle = new CmdLineAppBundle();
- }
-
- }
-
- public boolean isRegistered(String option) {
- return this.argumentParser.isRegistered(option);
- }
-
- public String helpString() {
- String useString = this.displayBundle.getString("option") + this.getAppName() + "\n";
- int MaxLength = 0;
- Enumeration enumKeys = this.argumentParser.getRegisteredValues().elements();
-
- while(enumKeys.hasMoreElements()) {
- Object object = enumKeys.nextElement();
- if (object instanceof RegisteredOption) {
- RegisteredOption option = (RegisteredOption)object;
- if (option.getShouldDisplay() && MaxLength < option.getOption().length()) {
- MaxLength = option.getOption().length();
- }
- }
- }
-
- Enumeration enumKeys2 = this.argumentParser.getRegisteredValues().elements();
-
- while(enumKeys2.hasMoreElements()) {
- Object object = enumKeys2.nextElement();
- if (object instanceof RegisteredOption) {
- RegisteredOption option = (RegisteredOption)object;
- if (option.getShouldDisplay()) {
- StringBuffer strbuf = new StringBuffer();
- int iPad = MaxLength - option.getOption().length() + 3;
-
- for(int x = 0; x < iPad; ++x) {
- strbuf.append(' ');
- }
-
- useString = useString + "[-" + option.getOption() + strbuf + option.getSuccinctExplanation() + ": " + option.getVerboseExplanation() + "]\n";
- }
- }
- }
-
- if (this.getRemainingMeaning() != null && this.getRemainingVerbose() != null) {
- useString = useString + "[" + this.getRemainingMeaning() + " " + this.getRemainingVerbose() + "]";
- }
-
- return useString;
- }
-
- protected void registerRemaining(String Meaning, String succinctExplain, String verboseExplain) {
- this.remainingMeaning = Meaning;
- this.remainingExplainSuccinct = succinctExplain;
- this.remainingExplainVerbose = verboseExplain;
- }
-
- public String getAppName() {
- return this.appName;
- }
-
- protected String getRemainingVerbose() {
- return this.remainingExplainVerbose;
- }
-
- protected String getRemainingMeaning() {
- return this.remainingMeaning;
- }
-
- public final void processOptions() throws CmdLineException {
- if (this.argumentParser != null && !this.argumentParser.isEmpty()) {
- Enumeration enumKeys = this.argumentParser.keys();
-
- while(enumKeys.hasMoreElements()) {
- String option = (String)enumKeys.nextElement();
- if (option == null) {
- throw new CmdLineException("BadOptionType", "Options must be of type string");
- }
-
- if (!this.isRegistered(option)) {
- throw new CmdLineInvalidOption(option, "OptionNotFound", "Invalid option specified");
- }
- }
-
- enumKeys = this.argumentParser.keys();
-
- while(enumKeys.hasMoreElements()) {
- String option = (String)enumKeys.nextElement();
- String value = (String)this.argumentParser.get(option);
- if (option == null) {
- throw new CmdLineException("BadOptionType", "Options must be of type string");
- }
-
- if (value == null) {
- throw new CmdLineException("BadValueType", "Values must be of type string");
- }
-
- this.processOption(option, value);
- }
- }
-
- }
-
- public abstract void processOption(String var1, String var2);
-
- public abstract void processApplication(String[] var1);
-
- public final void run(String[] args) throws CmdLineException {
- this.argumentParser.init(args);
- this.processOptions();
- this.processApplication(this.argumentParser.getRemainingArgs());
- }
-
- protected String getRemainingSuccinct() {
- return this.remainingExplainSuccinct;
- }
- }
-