~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/core/ListOptions.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or
 
5
 *    (at your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful,
 
8
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *    GNU General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 */
 
16
 
 
17
/*
 
18
 * ListOptions.java
 
19
 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
 
20
 */
 
21
 
 
22
package weka.core;
 
23
 
 
24
import java.util.Enumeration;
 
25
import java.util.Vector;
 
26
 
 
27
/**
 
28
 * Lists the options of an OptionHandler
 
29
 * 
 
30
 * @author  fracpete (fracpete at waikato dot ac dot nz)
 
31
 * @version $Revision: 1.1 $
 
32
 */
 
33
public class ListOptions
 
34
  implements OptionHandler {
 
35
  
 
36
  /** the classname */
 
37
  protected String m_Classname = ListOptions.class.getName();
 
38
  
 
39
  /**
 
40
   * Returns an enumeration describing the available options.
 
41
   *
 
42
   * @return an enumeration of all the available options.
 
43
   */
 
44
  public Enumeration listOptions() {
 
45
    Vector result = new Vector();
 
46
 
 
47
    result.addElement(new Option(
 
48
        "\tThe class to load.",
 
49
        "W", 1, "-W <classname>"));
 
50
    
 
51
    return result.elements();
 
52
  }
 
53
  
 
54
  /**
 
55
   * Parses a given list of options. 
 
56
   *
 
57
   * @param options the list of options as an array of strings
 
58
   * @throws Exception if an option is not supported
 
59
   */
 
60
  public void setOptions(String[] options) throws Exception {
 
61
    String                      tmpStr;
 
62
    
 
63
    tmpStr = Utils.getOption('W', options);
 
64
    if (tmpStr.length() > 0)
 
65
      setClassname(tmpStr);
 
66
    else
 
67
      setClassname(this.getClass().getName());
 
68
  }
 
69
  
 
70
  /**
 
71
   * Gets the current settings of this object.
 
72
   *
 
73
   * @return an array of strings suitable for passing to setOptions
 
74
   */
 
75
  public String[] getOptions() {
 
76
    Vector      result;
 
77
 
 
78
    result = new Vector();
 
79
    
 
80
    result.add("-W");
 
81
    result.add(getClassname());
 
82
    
 
83
    return (String[]) result.toArray(new String[result.size()]);
 
84
  }
 
85
  
 
86
  /**
 
87
   * sets the classname of the class to generate the Javadoc for
 
88
   * 
 
89
   * @param value       the new classname
 
90
   */
 
91
  public void setClassname(String value) {
 
92
    m_Classname = value;
 
93
  }
 
94
  
 
95
  /**
 
96
   * returns the current classname
 
97
   * 
 
98
   * @return    the current classname
 
99
   */
 
100
  public String getClassname() {
 
101
    return m_Classname;
 
102
  }
 
103
  
 
104
  /**
 
105
   * generates a string to print as help on the console
 
106
   * 
 
107
   * @return    the generated help
 
108
   */
 
109
  public String generateHelp() {
 
110
    String              result;
 
111
    Enumeration         enm;
 
112
    Option              option;
 
113
    
 
114
    result = getClass().getName().replaceAll(".*\\.", "") + " Options:\n\n";
 
115
    enm = listOptions();
 
116
    while (enm.hasMoreElements()) {
 
117
      option = (Option) enm.nextElement();
 
118
      result += option.synopsis() + "\n" + option.description() + "\n";
 
119
    }
 
120
    
 
121
    return result;
 
122
  }
 
123
  
 
124
  /**
 
125
   * generates the options string.
 
126
   * 
 
127
   * @return            the options string
 
128
   * @throws Exception  in case the generation fails
 
129
   */
 
130
  public String generate() throws Exception {
 
131
    StringBuffer        result;
 
132
    OptionHandler       handler;
 
133
    Enumeration         enm;
 
134
    Option              option;
 
135
    
 
136
    result = new StringBuffer();
 
137
    
 
138
    handler = (OptionHandler) Class.forName(getClassname()).newInstance();
 
139
    
 
140
    enm = ((OptionHandler) handler).listOptions();
 
141
    while (enm.hasMoreElements()) {
 
142
      option = (Option) enm.nextElement();
 
143
      result.append(option.synopsis() + '\n');
 
144
      result.append(option.description() + "\n");
 
145
    }
 
146
    
 
147
    return result.toString();
 
148
  }
 
149
  
 
150
  /**
 
151
   * runs the javadoc producer with the given commandline options
 
152
   * 
 
153
   * @param options     the commandline options
 
154
   */
 
155
  public static void main(String[] options) {
 
156
    ListOptions list = new ListOptions();
 
157
    
 
158
    try {
 
159
      try {
 
160
        if (Utils.getFlag('h', options))
 
161
          throw new Exception("Help requested");
 
162
 
 
163
        list.setOptions(options);
 
164
        Utils.checkForRemainingOptions(options);
 
165
      } 
 
166
      catch (Exception ex) {
 
167
        String result = "\n" + ex.getMessage() + "\n\n" + list.generateHelp();
 
168
        throw new Exception(result);
 
169
      }
 
170
 
 
171
      System.out.println("\n" + list.generate());
 
172
    } 
 
173
    catch (Exception ex) {
 
174
      System.err.println(ex.getMessage());
 
175
    }
 
176
  }
 
177
}