OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [getopt/] [OptionGroup.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* OptionGroup.java - a group of related command-line options
2
 Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
 This file is part of GNU Classpath.
5
 
6
 GNU Classpath is free software; you can redistribute it and/or modify
7
 it under the terms of the GNU General Public License as published by
8
 the Free Software Foundation; either version 2, or (at your option)
9
 any later version.
10
 
11
 GNU Classpath is distributed in the hope that it will be useful, but
12
 WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 General Public License for more details.
15
 
16
 You should have received a copy of the GNU General Public License
17
 along with GNU Classpath; see the file COPYING.  If not, write to the
18
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 02110-1301 USA.
20
 
21
 Linking this library statically or dynamically with other modules is
22
 making a combined work based on this library.  Thus, the terms and
23
 conditions of the GNU General Public License cover the whole
24
 combination.
25
 
26
 As a special exception, the copyright holders of this library give you
27
 permission to link this library with independent modules to produce an
28
 executable, regardless of the license terms of these independent
29
 modules, and to copy and distribute the resulting executable under
30
 terms of your choice, provided that you also meet, for each linked
31
 independent module, the terms and conditions of the license of that
32
 module.  An independent module is a module which is not derived from
33
 or based on this library.  If you modify this library, you may extend
34
 this exception to your version of the library, but you are not
35
 obligated to do so.  If you do not wish to do so, delete this
36
 exception statement from your version. */
37
 
38
 
39
package gnu.classpath.tools.getopt;
40
 
41
import java.io.PrintStream;
42
import java.text.BreakIterator;
43
import java.util.ArrayList;
44
import java.util.Iterator;
45
import java.util.Locale;
46
 
47
/**
48
 * An option group holds a collection of Options. It also has a name. Option
49
 * groups are primarily useful for grouping help output.
50
 */
51
public class OptionGroup
52
{
53
  /** An 80-character string of whitespaces to use as a source for padding. */
54
  private static final String FILLER = "                                        "
55
                                     + "                                        ";
56
  private String name;
57
 
58
  ArrayList options = new ArrayList();
59
 
60
  /**
61
   * Create a new nameless option group. This can only be used by Parser.
62
   */
63
  OptionGroup()
64
  {
65
  }
66
 
67
  /**
68
   * Create a new option group with the indicated name.
69
   *
70
   * @param name the name
71
   */
72
  public OptionGroup(String name)
73
  {
74
    this.name = name;
75
  }
76
 
77
  /**
78
   * Print a designated text to a {@link PrintStream}, eventually wrapping the
79
   * lines of text so as to ensure that the width of each line does not overflow
80
   * {@link Parser#MAX_LINE_LENGTH} columns. The line-wrapping is done with a
81
   * {@link BreakIterator} using the default {@link Locale}.
82
   * <p>
83
   * The text to print may contain <code>\n</code> characters. This method will
84
   * force a line-break for each such character.
85
   *
86
   * @param out the {@link PrintStream} destination of the formatted text.
87
   * @param text the text to print.
88
   * @param leftMargin a positive value indicating the column position of the
89
   *          start of the first line. Continuation lines, if they exist, are
90
   *          printed starting at <code>leftMargin + 2</code> as per GNU
91
   *          convention.
92
   * @see Parser#MAX_LINE_LENGTH
93
   */
94
  protected static void formatText(PrintStream out, String text, int leftMargin)
95
  {
96
    formatText(out, text, leftMargin, Locale.getDefault());
97
  }
98
 
99
  /**
100
   * Similar to the method with the same name and three arguments, except that
101
   * the caller MUST specify a non-null {@link Locale} instance.
102
   * <p>
103
   * Print a designated text to a {@link PrintStream}, eventually wrapping the
104
   * lines of text so as to ensure that the width of each line does not overflow
105
   * {@link Parser#MAX_LINE_LENGTH} columns. The line-wrapping is done with a
106
   * {@link BreakIterator} using the designated {@link Locale}.
107
   * <p>
108
   * The text to print may contain <code>\n</code> characters. This method will
109
   * force a line-break for each such character.
110
   *
111
   * @param out the {@link PrintStream} destination of the formatted text.
112
   * @param text the text to print.
113
   * @param leftMargin a positive value indicating the column position of the
114
   *          start of the first line. Continuation lines, if they exist, are
115
   *          printed starting at <code>leftMargin + 2</code> as per GNU
116
   *          convention.
117
   * @param aLocale the {@link Locale} instance to use when constructing the
118
   *          {@link BreakIterator}.
119
   * @see Parser#MAX_LINE_LENGTH
120
   */
121
  protected static void formatText(PrintStream out, String text, int leftMargin,
122
                                   Locale aLocale)
123
  {
124
    BreakIterator bit = BreakIterator.getLineInstance(aLocale);
125
    String[] lines = text.split("\n");
126
    int length = leftMargin;
127
    String leftPadding = FILLER.substring(0, leftMargin + 2);
128
    for (int i = 0; i < lines.length; i++)
129
      {
130
        text = lines[i];
131
        bit.setText(text);
132
        int start = bit.first();
133
        int finish;
134
        while ((finish = bit.next()) != BreakIterator.DONE)
135
          {
136
            String word = text.substring(start, finish);
137
            length += word.length();
138
            if (length >= Parser.MAX_LINE_LENGTH)
139
              {
140
                out.println();
141
                out.print(leftPadding);
142
                length = word.length() + leftMargin + 2;
143
              }
144
            out.print(word);
145
            start = finish;
146
          }
147
        out.println();
148
        if (i != lines.length - 1)
149
          {
150
            length = leftMargin + 2;
151
            out.print(leftPadding);
152
          }
153
      }
154
  }
155
 
156
  /**
157
   * Add an option to this option group.
158
   *
159
   * @param opt the option to add
160
   */
161
  public void add(Option opt)
162
  {
163
    options.add(opt);
164
  }
165
 
166
  /**
167
   * Print the help output for this option group.
168
   *
169
   * @param out the stream to which to print
170
   */
171
  public void printHelp(PrintStream out, boolean longOnly)
172
  {
173
    // Compute maximum lengths.
174
    int maxArgLen = 0;
175
    boolean shortOptionSeen = false;
176
    Iterator it;
177
 
178
    // The first pass only looks to see if we have a short option.
179
    it = options.iterator();
180
    while (it.hasNext())
181
      {
182
        Option option = (Option) it.next();
183
        if (option.getShortName() != '\0')
184
          {
185
            shortOptionSeen = true;
186
            break;
187
          }
188
      }
189
 
190
    it = options.iterator();
191
    while (it.hasNext())
192
      {
193
        Option option = (Option) it.next();
194
        String argName = option.getArgumentName();
195
        // First compute the width required for the short
196
        // option. "2" is the initial indentation. In the
197
        // GNU style we don't print an argument name for
198
        // a short option if there is also a long name for
199
        // the option.
200
        int thisArgLen = 2;
201
        if (shortOptionSeen)
202
          thisArgLen += 4;
203
        if (option.getLongName() != null)
204
          {
205
            // Handle either '-' or '--'.
206
            thisArgLen += 1 + option.getLongName().length();
207
            if (! longOnly)
208
              ++thisArgLen;
209
          }
210
        // Add in the width of the argument name.
211
        if (argName != null)
212
          thisArgLen += 1 + argName.length();
213
        maxArgLen = Math.max(maxArgLen, thisArgLen);
214
      }
215
 
216
    // Print the help.
217
    if (name != null)
218
      out.println(name + ":");
219
    it = options.iterator();
220
    while (it.hasNext())
221
      {
222
        Option option = (Option) it.next();
223
        String argName = option.getArgumentName();
224
        int column = 0;
225
        if (option.getShortName() != '\0')
226
          {
227
            out.print("  -");
228
            out.print(option.getShortName());
229
            column += 4;
230
            if (option.getLongName() == null)
231
              {
232
                if (argName != null)
233
                  {
234
                    if (! option.isJoined())
235
                      {
236
                        out.print(' ');
237
                        ++column;
238
                      }
239
                    out.print(argName);
240
                    column += argName.length();
241
                  }
242
                out.print("  ");
243
              }
244
            else
245
              out.print(", ");
246
            column += 2;
247
          }
248
        // Indent the long option past the short options, if one
249
        // was seen.
250
        for (; column < (shortOptionSeen ? 6 : 2); ++column)
251
          out.print(' ');
252
        if (option.getLongName() != null)
253
          {
254
            out.print(longOnly ? "-" : "--");
255
            out.print(option.getLongName());
256
            column += (longOnly ? 1 : 2) + option.getLongName().length();
257
            if (argName != null)
258
              {
259
                out.print(" " + argName);
260
                column += 1 + argName.length();
261
              }
262
          }
263
        // FIXME: should have a better heuristic for padding.
264
        out.print(FILLER.substring(0, maxArgLen + 4 - column));
265
        formatText(out, option.getDescription(), maxArgLen + 4);
266
      }
267
  }
268
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.