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

com.helger.cli.HelpFormatter Maven / Gradle / Ivy

There is a newer version: 11.1.8
Show newest version
/*
 * Original copyright by Apache Software Foundation
 * Copyright (C) 2017-2022 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * 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.helger.cli;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Iterator;

import javax.annotation.CheckForSigned;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.CommonsHashSet;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.collection.impl.ICommonsSet;
import com.helger.commons.compare.IComparator;
import com.helger.commons.io.stream.NonBlockingBufferedReader;
import com.helger.commons.io.stream.NonBlockingStringReader;
import com.helger.commons.string.StringHelper;
import com.helger.commons.system.ENewLineMode;

/**
 * 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
 * 
*/ public class HelpFormatter { /** 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 = CmdLineParser.PREFIX_SHORT_OPT; /** default prefix for long Option */ public static final String DEFAULT_LONG_OPT_PREFIX = CmdLineParser.PREFIX_LONG_OPT; /** * default separator displayed between a long Option and its value **/ public static final String DEFAULT_LONG_OPT_SEPARATOR = " "; /** default name for an argument */ public static final String DEFAULT_ARG_NAME = "arg"; /** * number of characters per line */ private int m_nDefaultWidth = DEFAULT_WIDTH; /** * amount of padding to the left of each line */ private int m_nDefaultLeftPad = DEFAULT_LEFT_PAD; /** * the number of characters of padding to be prefixed to each description line */ private int m_nDefaultDescPad = DEFAULT_DESC_PAD; /** * the string to display at the beginning of the usage statement */ private String m_sDefaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX; /** * the new line string */ private String m_sDefaultNewLine = ENewLineMode.DEFAULT.getText (); /** * the shortOpt prefix */ private String m_sOptPrefix = DEFAULT_OPT_PREFIX; /** * the long Opt prefix */ private String m_sLongOptPrefix = DEFAULT_LONG_OPT_PREFIX; /** * the name of the argument */ private String m_sArgName = 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 IComparator




© 2015 - 2024 Weber Informatics LLC | Privacy Policy