usr/src/java/util/org/opensolaris/os/vp/util/cli/HelpFormatter.java
author Stephen Talley <stephen.talley@oracle.com>
Mon, 28 Mar 2011 10:53:34 -0400
changeset 685 767674b0a2fb
parent 649 1ecbb3ac115d
permissions -rw-r--r--
18094 s/StringBuffer/StringBuilder/g
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     1
/*
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     2
 * CDDL HEADER START
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     3
 *
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     5
 * Common Development and Distribution License (the "License").
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     6
 * You may not use this file except in compliance with the License.
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     7
 *
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    11
 * and limitations under the License.
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    12
 *
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    18
 *
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    19
 * CDDL HEADER END
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    20
 */
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    21
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    22
/*
649
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
    23
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    24
 */
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    25
219
57841c113efe 6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents: 165
diff changeset
    26
package org.opensolaris.os.vp.util.cli;
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    27
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    28
import java.util.*;
591
7c5752b99fd6 12473 panels should probably be run in a sandbox
Stephen Talley <stephen.talley@oracle.com>
parents: 278
diff changeset
    29
import org.opensolaris.os.vp.util.misc.TextUtil;
7c5752b99fd6 12473 panels should probably be run in a sandbox
Stephen Talley <stephen.talley@oracle.com>
parents: 278
diff changeset
    30
import org.opensolaris.os.vp.util.misc.finder.Finder;
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    31
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    32
public class HelpFormatter {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    33
    //
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    34
    // Instance data
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    35
    //
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    36
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    37
    private String description;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    38
    private UsageFormatter formatter;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    39
    private String optionsLabel = Finder.getString("command.help.options");
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    40
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    41
    //
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    42
    // Constructors
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    43
    //
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    44
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    45
    public HelpFormatter(String description, UsageFormatter formatter) {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    46
	this.description = description;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    47
	this.formatter = formatter;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    48
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    49
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    50
    //
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    51
    // HelpFormatter methods
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    52
    //
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    53
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    54
    public String getDescription() {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    55
	return description;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    56
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    57
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    58
    public UsageFormatter getUsageFormatter() {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    59
	return formatter;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    60
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    61
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    62
    public void setOptionsLabel(String optionsLabel) {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    63
	this.optionsLabel = optionsLabel;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    64
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    65
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    66
    public String getOptionsLabel() {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    67
	return optionsLabel;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    68
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    69
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    70
    public String getHelp() {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    71
	UsageFormatter usage = getUsageFormatter();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    72
	OptionElement[] options = getOptionElements();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    73
	OptionFormatter formatter = usage.getOptionFormatter();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    74
685
767674b0a2fb 18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents: 649
diff changeset
    75
	StringBuilder buffer = new StringBuilder();
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    76
	int width = usage.getWidth();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    77
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    78
	buffer.append(usage.getUsage()).
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    79
	    append("\n\n").
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    80
	    append(TextUtil.format(getDescription(), width, "", "", false));
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    81
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    82
	if (options.length > 0) {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    83
	    int indentlen = usage.getIndent();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    84
	    String indent2 = String.format("%" + indentlen + "s", "");
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    85
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    86
	    String optionsLabel = getOptionsLabel();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    87
	    if (optionsLabel != null) {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    88
		buffer.append("\n\n").append(getOptionsLabel());
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    89
	    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    90
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    91
	    for (OptionElement option : options) {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    92
		String synopsis = formatter.getFormatted(option, true);
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    93
		buffer.append("\n\n");
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    94
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    95
		String indent1;
278
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
    96
		String description = option.getDescription();
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
    97
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    98
		// Can synopsis and description be on the same line?
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
    99
		if (synopsis.length() + 1 <= indentlen) {
278
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   100
		    description = String.format("%-" + indentlen + "s%s",
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   101
			synopsis, description);
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   102
		    indent1 = "";
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   103
		} else {
278
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   104
		    buffer.append(synopsis);
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   105
		    if (!description.isEmpty()) {
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   106
			buffer.append("\n");
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   107
		    }
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   108
		    indent1 = indent2;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   109
		}
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   110
278
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   111
		buffer.append(TextUtil.format(description, width, indent1,
9a605b67ca22 8128 short command-line options are not formatted correctly in help
Stephen Talley <stephen.talley@sun.com>
parents: 219
diff changeset
   112
		    indent2, false));
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   113
	    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   114
	}
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   115
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   116
	return buffer.toString();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   117
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   118
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   119
    //
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   120
    // Private methods
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   121
    //
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   122
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   123
    private OptionElement[] getOptionElements() {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   124
	// Recursively retrieve all OptionElements
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   125
	HashMap<String, OptionElement> map =
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   126
	    new HashMap<String, OptionElement>();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   127
	getOptionElements(getUsageFormatter().getOption(), map);
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   128
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   129
	ArrayList<OptionElement> elements = new ArrayList<OptionElement>();
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   130
	elements.addAll(map.values());
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   131
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   132
	Collections.sort(elements,
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   133
	    new Comparator<OptionElement>() {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   134
		public int compare(OptionElement a, OptionElement b) {
649
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   135
		    String aName = a.getName();
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   136
		    String bName = b.getName();
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   137
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   138
		    int result = aName.toLowerCase().compareTo(
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   139
			bName.toLowerCase());
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   140
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   141
		    if (result == 0) {
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   142
			// Lowercase before uppercase
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   143
			result = bName.compareTo(aName);
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   144
		    }
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   145
1ecbb3ac115d 17821 vp --help should sort options without respect to case
Stephen Talley <stephen.talley@oracle.com>
parents: 591
diff changeset
   146
		    return result;
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   147
		}
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   148
	    });
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   149
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   150
	return elements.toArray(new OptionElement[elements.size()]);
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   151
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   152
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   153
    private void getOptionElements(
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   154
	Option option, HashMap<String, OptionElement> map) {
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   155
165
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   156
	if (option.isDocumented()) {
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   157
	    if (option instanceof OptionElement) {
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   158
		OptionElement element = (OptionElement)option;
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   159
		String key = element.getName() + element.getArgName();
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   160
		map.put(key, element);
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   161
	    } else
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   162
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   163
	    if (option instanceof OptionGroup) {
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   164
		for (Option o : ((OptionGroup)option).getOptions()) {
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   165
		    getOptionElements(o, map);
f2763cdf36b4 5076 undocumented command-line options should be supported
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
   166
		}
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   167
	    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   168
	}
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   169
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff changeset
   170
}