jdk/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/BuiltinHelpFormatter.java
author chegar
Tue, 26 Jan 2016 09:18:51 +0000
changeset 35377 462f93ab37f2
permissions -rw-r--r--
8148154: Integrate JOpt Simple for internal usage by JDK tools Reviewed-by: alanb, redestad, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     1
/*
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     2
 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     4
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    10
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    15
 * accompanied this code).
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    16
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    20
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    23
 * questions.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    24
 */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    25
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    26
/*
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    30
 * file:
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    31
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    32
 * The MIT License
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    33
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    34
 * Copyright (c) 2004-2014 Paul R. Holser, Jr.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    35
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    36
 * Permission is hereby granted, free of charge, to any person obtaining
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    37
 * a copy of this software and associated documentation files (the
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    38
 * "Software"), to deal in the Software without restriction, including
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    39
 * without limitation the rights to use, copy, modify, merge, publish,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    40
 * distribute, sublicense, and/or sell copies of the Software, and to
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    41
 * permit persons to whom the Software is furnished to do so, subject to
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    42
 * the following conditions:
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    43
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    44
 * The above copyright notice and this permission notice shall be
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    45
 * included in all copies or substantial portions of the Software.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    46
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    47
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    48
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    49
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    50
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    51
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    52
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    53
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    54
 */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    55
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    56
package jdk.internal.joptsimple;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    57
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    58
import java.util.Collection;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    59
import java.util.Comparator;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    60
import java.util.Iterator;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    61
import java.util.List;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    62
import java.util.Map;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    63
import java.util.Set;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    64
import java.util.TreeSet;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    65
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    66
import jdk.internal.joptsimple.internal.Rows;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    67
import jdk.internal.joptsimple.internal.Strings;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    68
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    69
import static jdk.internal.joptsimple.ParserRules.*;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    70
import static jdk.internal.joptsimple.internal.Classes.*;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    71
import static jdk.internal.joptsimple.internal.Strings.*;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    72
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    73
/**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    74
 * <p>A help formatter that allows configuration of overall row width and column separator width.</p>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    75
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    76
 * <p>The formatter produces a two-column output. The left column is for the options, and the right column for their
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    77
 * descriptions. The formatter will allow as much space as possible for the descriptions, by minimizing the option
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    78
 * column's width, no greater than slightly less than half the overall desired width.</p>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    79
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    80
 * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    81
 */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    82
public class BuiltinHelpFormatter implements HelpFormatter {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    83
    private final Rows nonOptionRows;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    84
    private final Rows optionRows;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    85
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    86
    /**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    87
     * Makes a formatter with a pre-configured overall row width and column separator width.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    88
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    89
    BuiltinHelpFormatter() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    90
        this( 80, 2 );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    91
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    92
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    93
    /**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    94
     * Makes a formatter with a given overall row width and column separator width.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    95
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    96
     * @param desiredOverallWidth how many characters wide to make the overall help display
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    97
     * @param desiredColumnSeparatorWidth how many characters wide to make the separation between option column and
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    98
     * description column
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    99
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   100
    public BuiltinHelpFormatter( int desiredOverallWidth, int desiredColumnSeparatorWidth ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   101
        nonOptionRows = new Rows( desiredOverallWidth * 2, 0 );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   102
        optionRows = new Rows( desiredOverallWidth, desiredColumnSeparatorWidth );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   103
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   104
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   105
    public String format( Map<String, ? extends OptionDescriptor> options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   106
        Comparator<OptionDescriptor> comparator =
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   107
            new Comparator<OptionDescriptor>() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   108
                public int compare( OptionDescriptor first, OptionDescriptor second ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   109
                    return first.options().iterator().next().compareTo( second.options().iterator().next() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   110
                }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   111
            };
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   112
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   113
        Set<OptionDescriptor> sorted = new TreeSet<OptionDescriptor>( comparator );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   114
        sorted.addAll( options.values() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   115
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   116
        addRows( sorted );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   117
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   118
        return formattedHelpOutput();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   119
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   120
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   121
    private String formattedHelpOutput() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   122
        StringBuilder formatted = new StringBuilder();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   123
        String nonOptionDisplay = nonOptionRows.render();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   124
        if ( !Strings.isNullOrEmpty( nonOptionDisplay ) )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   125
            formatted.append( nonOptionDisplay ).append( LINE_SEPARATOR );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   126
        formatted.append( optionRows.render() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   127
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   128
        return formatted.toString();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   129
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   130
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   131
    private void addRows( Collection<? extends OptionDescriptor> options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   132
        addNonOptionsDescription( options );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   133
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   134
        if ( options.isEmpty() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   135
            optionRows.add( "No options specified", "" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   136
        else {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   137
            addHeaders( options );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   138
            addOptions( options );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   139
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   140
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   141
        fitRowsToWidth();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   142
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   143
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   144
    private void addNonOptionsDescription( Collection<? extends OptionDescriptor> options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   145
        OptionDescriptor nonOptions = findAndRemoveNonOptionsSpec( options );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   146
        if ( shouldShowNonOptionArgumentDisplay( nonOptions ) ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   147
            nonOptionRows.add( "Non-option arguments:", "" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   148
            nonOptionRows.add(createNonOptionArgumentsDisplay(nonOptions), "");
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   149
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   150
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   151
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   152
    private boolean shouldShowNonOptionArgumentDisplay( OptionDescriptor nonOptions ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   153
        return !Strings.isNullOrEmpty( nonOptions.description() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   154
            || !Strings.isNullOrEmpty( nonOptions.argumentTypeIndicator() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   155
            || !Strings.isNullOrEmpty( nonOptions.argumentDescription() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   156
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   157
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   158
    private String createNonOptionArgumentsDisplay(OptionDescriptor nonOptions) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   159
        StringBuilder buffer = new StringBuilder();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   160
        maybeAppendOptionInfo( buffer, nonOptions );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   161
        maybeAppendNonOptionsDescription( buffer, nonOptions );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   162
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   163
        return buffer.toString();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   164
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   165
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   166
    private void maybeAppendNonOptionsDescription( StringBuilder buffer, OptionDescriptor nonOptions ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   167
        buffer.append( buffer.length() > 0 && !Strings.isNullOrEmpty( nonOptions.description() ) ? " -- " : "" )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   168
            .append( nonOptions.description() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   169
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   170
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   171
    private OptionDescriptor findAndRemoveNonOptionsSpec( Collection<? extends OptionDescriptor> options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   172
        for ( Iterator<? extends OptionDescriptor> it = options.iterator(); it.hasNext(); ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   173
            OptionDescriptor next = it.next();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   174
            if ( next.representsNonOptions() ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   175
                it.remove();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   176
                return next;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   177
            }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   178
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   179
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   180
        throw new AssertionError( "no non-options argument spec" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   181
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   182
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   183
    private void addHeaders( Collection<? extends OptionDescriptor> options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   184
        if ( hasRequiredOption( options ) ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   185
            optionRows.add("Option (* = required)", "Description");
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   186
            optionRows.add("---------------------", "-----------");
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   187
        } else {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   188
            optionRows.add("Option", "Description");
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   189
            optionRows.add("------", "-----------");
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   190
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   191
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   192
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   193
    private boolean hasRequiredOption( Collection<? extends OptionDescriptor> options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   194
        for ( OptionDescriptor each : options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   195
            if ( each.isRequired() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   196
                return true;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   197
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   198
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   199
        return false;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   200
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   201
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   202
    private void addOptions( Collection<? extends OptionDescriptor> options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   203
        for ( OptionDescriptor each : options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   204
            if ( !each.representsNonOptions() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   205
                optionRows.add( createOptionDisplay( each ), createDescriptionDisplay( each ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   206
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   207
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   208
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   209
    private String createOptionDisplay( OptionDescriptor descriptor ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   210
        StringBuilder buffer = new StringBuilder( descriptor.isRequired() ? "* " : "" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   211
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   212
        for ( Iterator<String> i = descriptor.options().iterator(); i.hasNext(); ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   213
            String option = i.next();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   214
            buffer.append( option.length() > 1 ? DOUBLE_HYPHEN : HYPHEN );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   215
            buffer.append( option );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   216
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   217
            if ( i.hasNext() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   218
                buffer.append( ", " );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   219
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   220
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   221
        maybeAppendOptionInfo( buffer, descriptor );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   222
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   223
        return buffer.toString();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   224
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   225
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   226
    private void maybeAppendOptionInfo( StringBuilder buffer, OptionDescriptor descriptor ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   227
        String indicator = extractTypeIndicator( descriptor );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   228
        String description = descriptor.argumentDescription();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   229
        if ( indicator != null || !isNullOrEmpty( description ) )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   230
            appendOptionHelp( buffer, indicator, description, descriptor.requiresArgument() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   231
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   232
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   233
    private String extractTypeIndicator( OptionDescriptor descriptor ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   234
        String indicator = descriptor.argumentTypeIndicator();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   235
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   236
        if ( !isNullOrEmpty( indicator ) && !String.class.getName().equals( indicator ) )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   237
            return shortNameOf( indicator );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   238
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   239
        return null;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   240
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   241
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   242
    private void appendOptionHelp( StringBuilder buffer, String typeIndicator, String description, boolean required ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   243
        if ( required )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   244
            appendTypeIndicator( buffer, typeIndicator, description, '<', '>' );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   245
        else
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   246
            appendTypeIndicator( buffer, typeIndicator, description, '[', ']' );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   247
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   248
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   249
    private void appendTypeIndicator( StringBuilder buffer, String typeIndicator, String description,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   250
                                      char start, char end ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   251
        buffer.append( ' ' ).append( start );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   252
        if ( typeIndicator != null )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   253
            buffer.append( typeIndicator );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   254
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   255
        if ( !Strings.isNullOrEmpty( description ) ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   256
            if ( typeIndicator != null )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   257
                buffer.append( ": " );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   258
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   259
            buffer.append( description );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   260
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   261
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   262
        buffer.append( end );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   263
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   264
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   265
    private String createDescriptionDisplay( OptionDescriptor descriptor ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   266
        List<?> defaultValues = descriptor.defaultValues();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   267
        if ( defaultValues.isEmpty() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   268
            return descriptor.description();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   269
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   270
        String defaultValuesDisplay = createDefaultValuesDisplay( defaultValues );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   271
        return ( descriptor.description() + ' ' + surround( "default: " + defaultValuesDisplay, '(', ')' ) ).trim();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   272
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   273
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   274
    private String createDefaultValuesDisplay( List<?> defaultValues ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   275
        return defaultValues.size() == 1 ? defaultValues.get( 0 ).toString() : defaultValues.toString();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   276
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   277
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   278
    private void fitRowsToWidth() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   279
        nonOptionRows.fitToWidth();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   280
        optionRows.fitToWidth();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   281
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   282
}