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

org.apache.commons.cli.HelpFormatter Maven / Gradle / Ivy

There is a newer version: 5.0.22
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.cli;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * A formatter of help messages for command line options.
 *
 * 

Example:

* *
 * Options options = new Options();
 * options.addOption(OptionBuilder.withLongOpt("file")
 *                                .withDescription("The file to be processed")
 *                                .hasArg()
 *                                .withArgName("FILE")
 *                                .isRequired()
 *                                .create('f'));
 * options.addOption(OptionBuilder.withLongOpt("version")
 *                                .withDescription("Print the version of the application")
 *                                .create('v'));
 * options.addOption(OptionBuilder.withLongOpt("help").create('h'));
 * 
 * String header = "Do something useful with an input file\n\n";
 * String footer = "\nPlease report issues at http://example.com/issues";
 * 
 * HelpFormatter formatter = new HelpFormatter();
 * formatter.printHelp("myapp", header, options, footer, true);
 * 
* * This produces the following output: * *
 * usage: myapp -f <FILE> [-h] [-v]
 * Do something useful with an input file
 * 
 *  -f,--file <FILE>   The file to be processed
 *  -h,--help
 *  -v,--version       Print the version of the application
 * 
 * Please report issues at http://example.com/issues
 * 
* * @version $Id: HelpFormatter.java 1677407 2015-05-03 14:31:12Z britter $ */ public class HelpFormatter { // --------------------------------------------------------------- Constants /** default number of characters per line */ public static final int DEFAULT_WIDTH = 74; /** default padding to the left of each line */ public static final int DEFAULT_LEFT_PAD = 1; /** 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 */ public static final String DEFAULT_SYNTAX_PREFIX = "usage: "; /** default prefix for shortOpts */ public static final String DEFAULT_OPT_PREFIX = "-"; /** default prefix for long Option */ public static final String DEFAULT_LONG_OPT_PREFIX = "--"; /** * default separator displayed between a long Option and its value * * @since 1.3 **/ public static final String DEFAULT_LONG_OPT_SEPARATOR = " "; /** default name for an argument */ public static final String DEFAULT_ARG_NAME = "arg"; // -------------------------------------------------------------- Attributes /** * number of characters per line * * @deprecated Scope will be made private for next major version * - use get/setWidth methods instead. */ @Deprecated public int defaultWidth = DEFAULT_WIDTH; /** * amount of padding to the left of each line * * @deprecated Scope will be made private for next major version * - use get/setLeftPadding methods instead. */ @Deprecated public int defaultLeftPad = DEFAULT_LEFT_PAD; /** * the number of characters of padding to be prefixed * to each description line * * @deprecated Scope will be made private for next major version * - use get/setDescPadding methods instead. */ @Deprecated public int defaultDescPad = DEFAULT_DESC_PAD; /** * the string to display at the beginning of the usage statement * * @deprecated Scope will be made private for next major version * - use get/setSyntaxPrefix methods instead. */ @Deprecated public String defaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX; /** * the new line string * * @deprecated Scope will be made private for next major version * - use get/setNewLine methods instead. */ @Deprecated public String defaultNewLine = System.getProperty("line.separator"); /** * the shortOpt prefix * * @deprecated Scope will be made private for next major version * - use get/setOptPrefix methods instead. */ @Deprecated public String defaultOptPrefix = DEFAULT_OPT_PREFIX; /** * the long Opt prefix * * @deprecated Scope will be made private for next major version * - use get/setLongOptPrefix methods instead. */ @Deprecated public String defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX; /** * the name of the argument * * @deprecated Scope will be made private for next major version * - use get/setArgName methods instead. */ @Deprecated public String defaultArgName = DEFAULT_ARG_NAME; /** * Comparator used to sort the options when they output in help text * * Defaults to case-insensitive alphabetical sorting by option key */ protected Comparator




© 2015 - 2024 Weber Informatics LLC | Privacy Policy