Package org.clapper.util.io

Examples of org.clapper.util.io.WordWrapWriter


     * @return the usage string, with embedded newlines
     */
    public String getUsageMessage(String prefixMsg, int maxLineLength)
    {
        StringWriter     buf = new StringWriter();
        WordWrapWriter   out;
        String[]         strings;
        int              i;
        int              maxParamLength = 0;
        int              maxOptionLength = 0;
        String           s;
        StringBuffer     usageLine = new StringBuffer();
        OptionInfo[]     options;
        OptionInfo       opt;
        Locale           locale = Locale.getDefault();

        if (maxLineLength <= 0)
            maxLineLength = Integer.MAX_VALUE;

        out = new WordWrapWriter(buf, maxLineLength);

        if (prefixMsg != null)
        {
            out.println();
            out.println(prefixMsg);
            out.println();
        }

        // Now, print the summary line.

        String commandName = usageInfo.getCommandName();
        if (commandName != null)
        {
            usageLine.append (commandName);
        }

        else
        {
            usageLine.append("java ");
            usageLine.append(getClass().getName());
        }

        usageLine.append(' ');
        usageLine.append(BundleUtil.getMessage(Package.BUNDLE_NAME,
                                               locale,
                                               "CommandLineUtility.options1",
                                               "[options]"));
        usageLine.append(' ');

        // Add the parameter placeholders. We'll also calculate the maximum
        // parameter name length in this loop, to save an iteration later.

        strings = usageInfo.getParameterNames();
        if (strings.length > 0)
        {
            for (i = 0; i < strings.length; i++)
            {
                usageLine.append(' ');

                boolean optional = true;
                if (usageInfo.parameterIsRequired(strings[i]))
                    optional = false;

                if (optional)
                    usageLine.append('[');
                usageLine.append(strings[i]);
                if (optional)
                    usageLine.append(']');
                maxParamLength = Math.max(maxParamLength,
                                          strings[i].length() + 1);
            }
        }

        if ( (s = usageInfo.getUsagePrologue()) != null)
            out.println(s);

        s = BundleUtil.getMessage(Package.BUNDLE_NAME,
                                  locale,
                                  "CommandLineUtility.usage",
                                  "Usage:");
        out.setPrefix(s + " ");
        out.println(usageLine.toString());
        out.setPrefix (null);
        out.println();

        // Find the largest option name.

        out.println(BundleUtil.getMessage(Package.BUNDLE_NAME,
                                          locale,
                                          "CommandLineUtility.options2",
                                          "OPTIONS:"));
        out.println();

        maxOptionLength = 2;
        options = usageInfo.getOptions();
        for (i = 0; i < options.length; i++)
        {
            opt = options[i];

            // An option with a null explanation is hidden.

            if (opt.explanation == null)
                continue;

            if (opt.longOption != null)
            {
                // Allow room for short option, long option and argument,
                // if any.
                //
                // -x, --long-x <arg>

                int    len = 0;
                String sep = "";
                if (opt.shortOption != UsageInfo.NO_SHORT_OPTION)
                {
                    len = 2;    // -x
                    sep = ", ";
                }

                if (opt.longOption != null)
                {
                    len += (sep.length()
                        + UsageInfo.LONG_OPTION_PREFIX.length()
                        + opt.longOption.length());
                }

                if (opt.argToken != null)
                    len += (opt.argToken.length() + 1);

                maxOptionLength = Math.max(maxOptionLength, len + 1);
            }
        }

        if (maxOptionLength > MAX_OPTION_STRING_LENGTH)
            maxOptionLength = MAX_OPTION_STRING_LENGTH;

        // Now, print the options.

        StringBuffer optString = new StringBuffer();
        for (i = 0; i < options.length; i++)
        {
            opt = options[i];

            // An option with a null explanation is hidden.

            if (opt.explanation == null)
                continue;

            // If there's a short option, print it first. Then do the
            // long one.

            optString.setLength (0);
            String sep = "";

            if (opt.shortOption != UsageInfo.NO_SHORT_OPTION)
            {
                optString.append(UsageInfo.SHORT_OPTION_PREFIX);
                optString.append(opt.shortOption);
                sep = ", ";
            }

            if (opt.longOption != null)
            {
                optString.append(sep);
                optString.append(UsageInfo.LONG_OPTION_PREFIX);
                optString.append(opt.longOption);
            }

            if (opt.argToken != null)
            {
                optString.append(' ');
                optString.append(opt.argToken);
            }

            s = optString.toString();
            if (s.length() > maxOptionLength)
            {
                out.println (s);
                out.setPrefix(padString(" ", maxOptionLength));
            }

            else
            {
                out.setPrefix(padString(optString.toString(),
                                        maxOptionLength));
            }

            out.println(opt.explanation);
            out.setPrefix(null);
        }

        // Print the parameters. We already have size of the the largest
        // parameter name.

        strings = usageInfo.getParameterNames();
        if (strings.length > 0)
        {
            out.println();
            out.println(BundleUtil.getMessage(Package.BUNDLE_NAME,
                                              locale,
                                              "CommandLineUtility.params",
                                              "PARAMETERS:"));
            out.println();

            // Now, print the parameters.

            for (i = 0; i < strings.length; i++)
            {
                out.setPrefix(padString(strings[i], maxParamLength));
                out.println(usageInfo.getParameterExplanation(strings[i]));
                out.setPrefix(null);
            }
        }

        if ( (s = usageInfo.getUsageTrailer()) != null)
        {
            out.println();
            out.println(s);
        }

        out.flush();

        return buf.toString();
    }
View Full Code Here

TOP

Related Classes of org.clapper.util.io.WordWrapWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.