All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspectran.shell.command.option.HelpFormatter Maven / Gradle / Ivy

There is a newer version: 8.1.5
Show newest version
/*
 * Copyright (c) 2008-2023 The Aspectran Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aspectran.shell.command.option;

import com.aspectran.core.util.StringUtils;
import com.aspectran.shell.command.Command;
import com.aspectran.shell.console.Console;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * A formatter of help messages for command line options.
 */
public class HelpFormatter {
    
    private static final String NEW_LINE = System.lineSeparator();

    /** Default number of characters per line */
    public static final int DEFAULT_WIDTH = 76;

    /** Default number of characters per line */
    public static final int DEFAULT_MAX_LEFT_WIDTH = 15;

    /** Default padding to the left of each line */
    public static final int DEFAULT_LEFT_PAD = 3;

    /** number of space characters to be prefixed to each description line */
    public static final int DEFAULT_DESC_PAD = 3;

    /** the string to display at the beginning of the usage statement */
    private static final String DEFAULT_SYNTAX_PREFIX = "Usage: ";

    /** Default name for an argument */
    private static final String DEFAULT_ARG_NAME = "arg";

    /** Default prefix for shortOpts */
    public static final String OPTION_PREFIX = "-";

    /** Default prefix for long Option */
    public static final String LONG_OPTION_PREFIX = "--";

    /** Default separator displayed between a long Option and its value */
    private static final String LONG_OPTION_SEPARATOR = " ";

    /** The opening pointy bracket to wrap an argument */
    private static final String ARG_BRACKET_OPEN = "<";

    /** The closing pointy bracket to wrap an argument */
    private static final String ARG_BRACKET_CLOSE = ">";

    /** The opening square bracket to indicate optional option or argument */
    private static final String OPTIONAL_BRACKET_OPEN = "[";

    /** The closing square bracket to indicate optional option or argument */
    private static final String OPTIONAL_BRACKET_CLOSE = "]";

    private int width = DEFAULT_WIDTH;

    private int maxLeftWidth = DEFAULT_MAX_LEFT_WIDTH;

    private int leftPad = DEFAULT_LEFT_PAD;

    private int descPad = DEFAULT_DESC_PAD;

    private String syntaxPrefix = DEFAULT_SYNTAX_PREFIX;

    private String argName = DEFAULT_ARG_NAME;

    /**
     * Comparator used to sort the options when they output in help text.
     * Defaults to case-insensitive alphabetical sorting by option key.
     */
    private Comparator




© 2015 - 2024 Weber Informatics LLC | Privacy Policy